เมื่อแอปไคลเอ็นต์ของคุณได้รับการติดตั้งบนอุปกรณ์แล้ว แอปจะสามารถรับข้อความผ่านอินเทอร์เฟซ FCM APN ได้ คุณสามารถเริ่มส่งการแจ้งเตือนไปยังกลุ่มผู้ใช้ได้ทันทีด้วยตัวเขียนการ แจ้งเตือน หรือข้อความที่สร้างบนเซิร์ฟเวอร์แอปพลิเคชันของคุณ
จัดการการแจ้งเตือน
FCM ส่งข้อความทั้งหมดที่กำหนดเป้าหมายแอป Apple ผ่าน APN หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับการรับการแจ้งเตือน APN ผ่าน UNUserNotificationCenter โปรดดูเอกสารประกอบของ Apple เกี่ยวกับ การจัดการการแจ้งเตือนและการดำเนินการที่เกี่ยวข้องกับการแจ้งเตือน
คุณต้องตั้งค่า ผู้รับมอบสิทธิ์ UNUserNotificationCenter และใช้วิธีการรับมอบสิทธิ์ที่เหมาะสมเพื่อรับการแจ้งเตือนการแสดงผลจาก FCM
Swift
@available(iOS 10, *) extension AppDelegate: UNUserNotificationCenterDelegate { // Receive displayed notifications for iOS 10 devices. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // ... // Print full message. print(userInfo) // Change this to your preferred presentation option completionHandler([[.alert, .sound]]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo // ... // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print full message. print(userInfo) completionHandler() } }
วัตถุประสงค์-C
// Receive displayed notifications for iOS 10 devices. // Handle incoming notification messages while app is in the foreground. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; // With swizzling disabled you must let Messaging know about the message, for Analytics // [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // ... // Print full message. NSLog(@"%@", userInfo); // Change this to your preferred presentation option completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert); } // Handle notification messages after display notification is tapped by the user. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; if (userInfo[kGCMMessageIDKey]) { NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); } // With swizzling disabled you must let Messaging know about the message, for Analytics // [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // Print full message. NSLog(@"%@", userInfo); completionHandler(); }
หากคุณต้องการเพิ่มการดำเนินการที่กำหนดเองให้กับการแจ้งเตือนของคุณ ให้ตั้งค่าพารามิเตอร์ click_action
ในเพย์ โหลดการแจ้งเตือน ใช้ค่าที่คุณจะใช้สำหรับคีย์ category
ในส่วนข้อมูล APN ต้องลงทะเบียนการดำเนินการที่กำหนดเองก่อนจึงจะสามารถใช้งานได้ สำหรับข้อมูลเพิ่มเติม โปรดดูที่ คู่มือการเขียนโปรแกรมการแจ้งเตือนในเครื่องและการแจ้งเตือนระยะไกล ของ Apple
สำหรับข้อมูลเชิงลึกเกี่ยวกับการส่งข้อความไปยังแอปของคุณ โปรดดู แดชบอร์ดการรายงาน FCM ซึ่งบันทึกจำนวนข้อความที่ส่งและเปิดบนอุปกรณ์ Apple และ Android พร้อมด้วยข้อมูลสำหรับ "การแสดงผล" (การแจ้งเตือนที่ผู้ใช้เห็น) สำหรับแอป Android
จัดการการแจ้งเตือนแบบพุชเงียบ
เมื่อส่งข้อความด้วยคีย์ content_available
(เทียบเท่ากับ content-available
ใน APNs ข้อความจะถูกส่งเป็นการแจ้งเตือนแบบไม่มีเสียง ปลุกแอปของคุณในเบื้องหลังสำหรับงานต่างๆ เช่น การรีเฟรชข้อมูลแบ็กกราวด์ การแจ้งเตือนเหล่านี้ต่างจากการแจ้งเตือนเบื้องหน้า การแจ้งเตือนเหล่านี้ต้องได้รับการจัดการผ่าน application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
วิธี
ใช้ application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
ดังที่แสดง:
Swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // 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) completionHandler(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); }
แพลตฟอร์มของ Apple ไม่รับประกันการส่งการแจ้งเตือนเบื้องหลัง หากต้องการเรียนรู้เกี่ยวกับเงื่อนไขที่อาจทำให้การแจ้งเตือนพื้นหลังล้มเหลว โปรดดูเอกสารของ Apple เกี่ยวกับ การส่งการอัปเดตพื้นหลังไปยังแอปของคุณ
การตีความ payload ของข้อความแจ้งเตือน
ส่วนของข้อความแจ้งเตือนเป็นพจนานุกรมของคีย์และค่าต่างๆ ข้อความแจ้งเตือนที่ส่งผ่าน APN จะเป็นไปตามรูปแบบเพย์โหลดของ APN ดังนี้:
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
จัดการข้อความโดยปิดการใช้งานวิธีการวนซ้ำ
ตามค่าเริ่มต้น หากคุณกำหนดคลาสผู้รับมอบสิทธิ์แอปของแอปให้กับคุณสมบัติของผู้รับมอบสิทธิ์ UNUserNotificationCenter
และ Messaging
FCM จะสลับคลาสผู้รับมอบสิทธิ์แอปของคุณเพื่อเชื่อมโยงโทเค็น FCM ของคุณกับโทเค็น APN ของอุปกรณ์โดยอัตโนมัติ และส่งผ่านเหตุการณ์ที่ได้รับการแจ้งเตือนไปยัง Analytics หากคุณปิดใช้งานวิธีการ swizzling อย่างชัดเจนหรือใช้คลาสแยกกันสำหรับผู้ได้รับมอบหมายรายใดรายหนึ่ง คุณจะต้องดำเนินการทั้งสองนี้ด้วยตนเอง
ในการเชื่อมโยงโทเค็น FCM กับโทเค็น APN ของอุปกรณ์ ให้ส่งโทเค็น APN ไปยังคลาส Messaging
ใน ตัวจัดการการรีเฟรชโทเค็น ของผู้รับมอบสิทธิ์แอปของคุณผ่าน คุณสมบัติ apnsToken
Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken; }
วัตถุประสงค์-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRMessaging messaging].APNSToken = deviceToken; }
ในการส่งข้อมูลใบรับการแจ้งเตือนไปยัง Analytics ให้ใช้ appDidReceiveMessage(_:)
Swift
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo Messaging.messaging().appDidReceiveMessage(userInfo) // Change this to your preferred presentation option completionHandler([[.alert, .sound]]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler() } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler(.noData) }
วัตถุประสงค์-C
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSDictionary *userInfo = notification.request.content.userInfo; [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // Change this to your preferred presentation option completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert); } - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; completionHandler(); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; completionHandler(UIBackgroundFetchResultNoData); }