ส่งข้อความไปยังอุปกรณ์หลายเครื่องบนแพลตฟอร์ม Apple

หากต้องการกำหนดเป้าหมายข้อความไปยังอุปกรณ์หลายเครื่อง ให้ใช้ ข้อความตามหัวข้อ คุณสมบัตินี้ช่วยให้คุณสามารถส่งข้อความไปยังอุปกรณ์หลายเครื่องที่เลือกหัวข้อใดหัวข้อหนึ่งได้

บทช่วยสอนนี้มุ่งเน้นไปที่การส่งข้อความหัวข้อจากเซิร์ฟเวอร์แอปของคุณโดยใช้ Admin SDK หรือ REST API สำหรับ FCM และการรับและจัดการข้อความเหล่านั้นในแอป Apple หน้านี้แสดงรายการขั้นตอนทั้งหมดเพื่อให้บรรลุเป้าหมายนี้ ตั้งแต่การตั้งค่าไปจนถึงการยืนยัน ดังนั้นจึงอาจครอบคลุมขั้นตอนที่คุณทำเสร็จแล้ว หากคุณได้ ตั้งค่าแอปไคลเอ็นต์ Apple สำหรับ FCM หรือดำเนินการตามขั้นตอนต่างๆ เพื่อ ส่งข้อความแรกของคุณ

เพิ่ม Firebase ให้กับโปรเจ็กต์ Apple ของคุณ

ส่วนนี้ครอบคลุมถึงงานที่คุณอาจทำเสร็จแล้วหากคุณได้เปิดใช้ฟีเจอร์ Firebase อื่นๆ สำหรับแอปของคุณแล้ว สำหรับ FCM โดยเฉพาะ คุณจะต้อง อัปโหลดคีย์การตรวจสอบสิทธิ์ APN และ ลงทะเบียนเพื่อรับการแจ้งเตือนระยะไกล

ข้อกำหนดเบื้องต้น

  • ติดตั้งสิ่งต่อไปนี้:

    • Xcode 14.1 หรือใหม่กว่า
  • ตรวจสอบให้แน่ใจว่าโครงการของคุณตรงตามข้อกำหนดเหล่านี้:

    • โครงการของคุณต้องกำหนดเป้าหมายเวอร์ชันแพลตฟอร์มเหล่านี้หรือใหม่กว่า:
      • ไอโอเอส 11
      • macOS10.13
      • ทีวีโอเอส 12
      • วอตช์โอเอส 6
  • ตั้งค่า อุปกรณ์ Apple จริง เพื่อเรียกใช้แอพของคุณและทำงานเหล่านี้ให้เสร็จสิ้น:

    • รับคีย์การรับรองความถูกต้องการแจ้งเตือน Apple Push สำหรับ บัญชี Apple Developer ของคุณ
    • เปิดใช้งานการแจ้งเตือนแบบพุชใน XCode ภายใต้ App > Capabilities

หากคุณยังไม่มีโปรเจ็กต์ Xcode และเพียงต้องการลองใช้ผลิตภัณฑ์ Firebase คุณสามารถดาวน์โหลดหนึ่งใน ตัวอย่างการเริ่มต้นอย่างรวดเร็ว ของเรา

สร้างโปรเจ็กต์ Firebase

ก่อนที่คุณจะเพิ่ม Firebase ลงในแอป Apple ได้ คุณต้องสร้างโปรเจ็กต์ Firebase เพื่อเชื่อมต่อกับแอปของคุณก่อน ไปที่ ทำความเข้าใจโปรเจ็กต์ Firebase เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับโปรเจ็กต์ Firebase

ลงทะเบียนแอปของคุณกับ Firebase

หากต้องการใช้ Firebase ในแอป Apple คุณต้องลงทะเบียนแอปกับโปรเจ็กต์ Firebase การลงทะเบียนแอปของคุณมักเรียกว่า "การเพิ่ม" แอปของคุณในโครงการของคุณ

  1. ไปที่ คอนโซล Firebase

  2. ที่กึ่งกลางของหน้าภาพรวมโครงการ ให้คลิกไอคอน iOS+ เพื่อเปิดเวิร์กโฟลว์การตั้งค่า

    หากคุณได้เพิ่มแอปลงในโปรเจ็กต์ Firebase แล้ว ให้คลิก เพิ่มแอป เพื่อแสดงตัวเลือกแพลตฟอร์ม

  3. ป้อนรหัสชุดของแอปของคุณในช่อง รหัสชุด

  4. (ไม่บังคับ) ป้อนข้อมูลแอปอื่นๆ: ชื่อเล่นแอป และ รหัส App Store

  5. คลิกลง ทะเบียนแอป

เพิ่มไฟล์การกำหนดค่า Firebase

  1. คลิก ดาวน์โหลด GoogleService-Info.plist เพื่อรับไฟล์กำหนดค่าแพลตฟอร์ม Firebase Apple ของคุณ ( GoogleService-Info.plist )

  2. ย้ายไฟล์ปรับแต่งของคุณไปที่รากของโปรเจ็กต์ Xcode ของคุณ หากได้รับแจ้ง ให้เลือกเพื่อเพิ่มไฟล์กำหนดค่าให้กับเป้าหมายทั้งหมด

หากคุณมี ID บันเดิลหลายรายการในโปรเจ็กต์ของคุณ คุณต้องเชื่อมโยง ID บันเดิลแต่ละรายการกับแอปที่ลงทะเบียนในคอนโซล Firebase เพื่อให้แต่ละแอปมีไฟล์ GoogleService-Info.plist ของตัวเองได้

เพิ่ม Firebase SDK ลงในแอปของคุณ

ใช้ Swift Package Manager เพื่อติดตั้งและจัดการการพึ่งพา Firebase

  1. ใน Xcode เมื่อโปรเจ็กต์แอปของคุณเปิดอยู่ ให้ไปที่ File > Add Package
  2. เมื่อได้รับแจ้ง ให้เพิ่มพื้นที่เก็บข้อมูล SDK แพลตฟอร์ม Firebase Apple:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. เลือกไลบรารี Firebase Cloud Messaging
  5. เพิ่มแฟล็ก -ObjC ไปยังส่วน Other Linker Flags ของการตั้งค่า build ของเป้าหมายของคุณ
  6. เพื่อประสบการณ์ที่ดีที่สุดกับ Firebase Cloud Messaging เราขอแนะนำ ให้เปิดใช้ Google Analytics ในโปรเจ็กต์ Firebase ของคุณ และเพิ่ม Firebase SDK สำหรับ Google Analytics ลงในแอปของคุณ คุณสามารถเลือกไลบรารีที่ไม่มีการรวบรวม IDFA หรือด้วยการรวบรวม IDFA
  7. เมื่อเสร็จแล้ว Xcode จะเริ่มแก้ไขและดาวน์โหลดการอ้างอิงของคุณโดยอัตโนมัติในเบื้องหลัง

อัปโหลดคีย์การตรวจสอบสิทธิ์ APN ของคุณ

อัปโหลดคีย์การตรวจสอบสิทธิ์ APN ของคุณไปยัง Firebase หากคุณยังไม่มีคีย์การตรวจสอบสิทธิ์ APN โปรดสร้างคีย์ดังกล่าวใน ศูนย์สมาชิกนักพัฒนา Apple

  1. ภายในโปรเจ็กต์ของคุณในคอนโซล Firebase ให้เลือกไอคอนรูปเฟือง เลือก การตั้งค่าโปรเจ็กต์ จากนั้นเลือกแท็บ Cloud Messaging

  2. ใน คีย์การตรวจสอบสิทธิ์ APN ใต้ การกำหนดค่าแอป iOS ให้คลิกปุ่ม อัปโหลด

  3. เรียกดูตำแหน่งที่คุณบันทึกกุญแจไว้ เลือกแล้วคลิก เปิด เพิ่ม ID คีย์สำหรับคีย์ (มีอยู่ใน Apple Developer Member Center ) แล้วคลิก อัปโหลด

เริ่มต้น Firebase ในแอปของคุณ

คุณจะต้องเพิ่มรหัสเริ่มต้น Firebase ให้กับแอปพลิเคชันของคุณ นำเข้าโมดูล Firebase และกำหนดค่าอินสแตนซ์ที่ใช้ร่วมกันตามที่แสดง:

  1. นำเข้าโมดูล FirebaseCore ใน UIApplicationDelegate ของคุณ รวมถึง โมดูล Firebase อื่นๆ ที่แอปของคุณมอบหมายใช้ ตัวอย่างเช่น หากต้องการใช้ Cloud Firestore และการตรวจสอบสิทธิ์ ให้ทำดังนี้

    SwiftUI

    import SwiftUI
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    สวิฟท์

    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    วัตถุประสงค์-C

    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  2. กำหนดค่าอินสแตนซ์ที่ใช้ร่วมกันของ FirebaseApp ใน application(_:didFinishLaunchingWithOptions:) ของผู้รับมอบสิทธิ์แอปของคุณ:

    SwiftUI

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    สวิฟท์

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    วัตถุประสงค์-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
  3. หากคุณใช้ SwiftUI คุณต้องสร้างผู้แทนแอปพลิเคชันและแนบไปกับโครงสร้าง App ของคุณผ่าน UIApplicationDelegateAdaptor หรือ NSApplicationDelegateAdaptor คุณต้องปิดใช้งานการสลับตัวแทนของแอปด้วย สำหรับข้อมูลเพิ่มเติม โปรดดู คำแนะนำ SwiftUI

    SwiftUI

    @main
    struct YourApp: App {
      // register app delegate for Firebase setup
      @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
      var body: some Scene {
        WindowGroup {
          NavigationView {
            ContentView()
          }
        }
      }
    }
          

ลงทะเบียนเพื่อรับการแจ้งเตือนระยะไกล

เมื่อเริ่มต้นระบบหรือ ณ จุดที่ต้องการในขั้นตอนการสมัครของคุณ ให้ลงทะเบียนแอปของคุณสำหรับการแจ้งเตือนระยะไกล โทร registerForRemoteNotifications ดังแสดง:

สวิฟท์


UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
  options: authOptions,
  completionHandler: { _, _ in }
)

application.registerForRemoteNotifications()

วัตถุประสงค์-C


[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
    UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
    requestAuthorizationWithOptions:authOptions
    completionHandler:^(BOOL granted, NSError * _Nullable error) {
      // ...
    }];

[application registerForRemoteNotifications];

สมัครสมาชิกแอปไคลเอนต์ในหัวข้อ

แอปไคลเอนต์สามารถสมัครรับหัวข้อที่มีอยู่หรือสามารถสร้างหัวข้อใหม่ได้ เมื่อแอปไคลเอนต์สมัครรับชื่อหัวข้อใหม่ (ที่ไม่มีอยู่แล้วสำหรับโปรเจ็กต์ Firebase ของคุณ) หัวข้อใหม่ของชื่อนั้นจะถูกสร้างขึ้นใน FCM และไคลเอนต์ใด ๆ ก็สามารถสมัครรับชื่อนั้นได้ในภายหลัง

หากต้องการสมัครรับหัวข้อ ให้เรียกวิธีการสมัครสมาชิกจากเธรดหลักของแอปพลิเคชันของคุณ (FCM ไม่ปลอดภัยสำหรับเธรด) หากคำขอสมัครสมาชิกล้มเหลวในตอนแรก FCM จะพยายามอีกครั้งโดยอัตโนมัติ สำหรับกรณีที่ไม่สามารถสมัครสมาชิกให้เสร็จสมบูรณ์ได้ การสมัครสมาชิกจะส่งข้อผิดพลาดที่คุณสามารถตรวจพบได้ในตัวจัดการความสมบูรณ์ดังที่แสดง:

สวิฟท์

Messaging.messaging().subscribe(toTopic: "weather") { error in
  print("Subscribed to weather topic")
}

วัตถุประสงค์-C

[[FIRMessaging messaging] subscribeToTopic:@"weather"
                                completion:^(NSError * _Nullable error) {
  NSLog(@"Subscribed to weather topic");
}];

การโทรนี้ส่งคำขอแบบอะซิงโครนัสไปยังแบ็กเอนด์ FCM และสมัครรับไคลเอ็นต์ในหัวข้อที่กำหนด ก่อนที่จะโทร subscribeToTopic:topic ตรวจสอบให้แน่ใจว่าอินสแตนซ์แอปไคลเอนต์ได้รับโทเค็นการลงทะเบียนแล้วผ่านการเรียกกลับ didReceiveRegistrationToken

ทุกครั้งที่แอปเริ่มต้น FCM จะตรวจสอบให้แน่ใจว่าได้สมัครรับหัวข้อที่ร้องขอทั้งหมดแล้ว หากต้องการยกเลิกการสมัคร ให้โทร unsubscribeFromTopic:topic และ FCM ยกเลิกการสมัครจากหัวข้อในเบื้องหลัง

รับและจัดการข้อความตามหัวข้อ

FCM ส่งข้อความหัวข้อในลักษณะเดียวกับข้อความดาวน์สตรีมอื่นๆ

ใช้งาน application(_:didReceiveRemoteNotification:fetchCompletionHandler:) ดังที่แสดง:

สวิฟท์

func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async
  -> UIBackgroundFetchResult {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // Messaging.messaging().appDidReceiveMessage(userInfo)

  // Print message ID.
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }

  // Print full message.
  print(userInfo)

  return UIBackgroundFetchResult.newData
}

วัตถุประสงค์-C

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // [[FIRMessaging messaging] appDidReceiveMessage:userInfo];

  // ...

  // Print full message.
  NSLog(@"%@", userInfo);

  completionHandler(UIBackgroundFetchResultNewData);
}

สร้างคำขอส่ง

หลังจากที่คุณสร้างหัวข้อแล้ว โดยการสมัครอินสแตนซ์แอปไคลเอ็นต์กับหัวข้อในฝั่งไคลเอ็นต์หรือผ่าน เซิร์ฟเวอร์ API คุณสามารถส่งข้อความไปยังหัวข้อได้ หากนี่เป็นครั้งแรกที่คุณสร้างส่งคำขอสำหรับ FCM โปรดดูคำแนะนำเกี่ยวกับ สภาพแวดล้อมเซิร์ฟเวอร์ของคุณและ FCM สำหรับข้อมูลพื้นหลังและการตั้งค่าที่สำคัญ

ในตรรกะการส่งของคุณบนแบ็กเอนด์ ให้ระบุชื่อหัวข้อที่ต้องการตามที่แสดง:

โหนด js

// The topic name can be optionally prefixed with "/topics/".
const topic = 'highScores';

const message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic
};

// Send a message to devices subscribed to the provided topic.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

ชวา

// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";

// See documentation on defining a message payload.
Message message = Message.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .setTopic(topic)
    .build();

// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

หลาม

# The topic name can be optionally prefixed with "/topics/".
topic = 'highScores'

# See documentation on defining a message payload.
message = messaging.Message(
    data={
        'score': '850',
        'time': '2:45',
    },
    topic=topic,
)

# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

ไป

// The topic name can be optionally prefixed with "/topics/".
topic := "highScores"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Topic: topic,
}

// Send a message to the devices subscribed to the provided topic.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

ค#

// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";

// See documentation on defining a message payload.
var message = new Message()
{
    Data = new Dictionary<string, string>()
    {
        { "score", "850" },
        { "time", "2:45" },
    },
    Topic = topic,
};

// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

พักผ่อน

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message"
      }
   }
}

คำสั่ง cURL:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "message": {
    "topic" : "foo-bar",
    "notification": {
      "body": "This is a Firebase Cloud Messaging Topic Message!",
      "title": "FCM Message"
    }
  }
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

หากต้องการส่งข้อความไปยังหัวข้อต่างๆ รวมกัน ให้ระบุ เงื่อนไข ซึ่งเป็นนิพจน์บูลีนที่ระบุหัวข้อเป้าหมาย ตัวอย่างเช่น เงื่อนไขต่อไปนี้จะส่งข้อความไปยังอุปกรณ์ที่สมัครรับ TopicA และ TopicB หรือ TopicC :

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

FCM จะประเมินเงื่อนไขใดๆ ในวงเล็บก่อน จากนั้นจึงประเมินนิพจน์จากซ้ายไปขวา ในนิพจน์ข้างต้น ผู้ใช้ที่สมัครรับข้อมูลหัวข้อใดหัวข้อหนึ่งจะไม่ได้รับข้อความ ในทำนองเดียวกัน ผู้ใช้ที่ไม่ได้สมัครสมาชิก TopicA จะไม่ได้รับข้อความ ชุดค่าผสมเหล่านี้ได้รับ:

  • TopicA และ TopicB
  • TopicA และ TopicC

คุณสามารถรวมหัวข้อได้สูงสุดห้าหัวข้อในนิพจน์แบบมีเงื่อนไขของคุณ

หากต้องการส่งตามเงื่อนไข:

โหนด js

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
const condition = '\'stock-GOOG\' in topics || \'industry-tech\' in topics';

// See documentation on defining a message payload.
const message = {
  notification: {
    title: '$FooCorp up 1.43% on the day',
    body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
  },
  condition: condition
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

ชวา

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
Message message = Message.builder()
    .setNotification(Notification.builder()
        .setTitle("$GOOG up 1.43% on the day")
        .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
        .build())
    .setCondition(condition)
    .build();

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

หลาม

# Define a condition which will send to devices which are subscribed
# to either the Google stock or the tech industry topics.
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"

# See documentation on defining a message payload.
message = messaging.Message(
    notification=messaging.Notification(
        title='$GOOG up 1.43% on the day',
        body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
    ),
    condition=condition,
)

# Send a message to devices subscribed to the combination of topics
# specified by the provided condition.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

ไป

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
condition := "'stock-GOOG' in topics || 'industry-tech' in topics"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Condition: condition,
}

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

ค#

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
var message = new Message()
{
    Notification = new Notification()
    {
        Title = "$GOOG up 1.43% on the day",
        Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
    },
    Condition = condition,
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

พักผ่อน

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
   "message":{
    "condition": "'dogs' in topics || 'cats' in topics",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message",
    }
  }
}

คำสั่ง cURL:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "notification": {
    "title": "FCM Message",
    "body": "This is a Firebase Cloud Messaging Topic Message!",
  },
  "condition": "'dogs' in topics || 'cats' in topics"
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

ขั้นตอนถัดไป