เมื่อติดตั้งแอปไคลเอ็นต์ในอุปกรณ์แล้ว แอปจะรับข้อความผ่าน FCMอินเทอร์เฟซ APNs ได้ คุณเริ่มส่งการแจ้งเตือนไปยังกลุ่มผู้ใช้ได้ทันทีด้วย เครื่องมือแต่งการแจ้งเตือน หรือข้อความที่สร้างขึ้นในเซิร์ฟเวอร์แอปพลิเคชัน
การรองรับ APNsจัดการการแจ้งเตือน
FCM จะส่งข้อความทั้งหมดที่กำหนดเป้าหมายเป็นแอปของ Apple ผ่าน APNs ดูข้อมูลเพิ่มเติม เกี่ยวกับการรับการแจ้งเตือน APNs ผ่าน UNUserNotificationCenter ได้ในเอกสารประกอบของ Apple เกี่ยวกับการจัดการการแจ้งเตือนและการดำเนินการที่เกี่ยวข้องกับการแจ้งเตือน
คุณต้องตั้งค่า ผู้มอบสิทธิ์ UNUserNotificationCenter และใช้เมธอดผู้มอบสิทธิ์ที่เหมาะสมเพื่อรับการแจ้งเตือนที่แสดง จาก FCM
Swift
extension AppDelegate: UNUserNotificationCenterDelegate { // Receive displayed notifications for iOS 10 devices. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions { 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 return [[.alert, .sound]] } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async { 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) } }
Objective-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
ในเพย์โหลด APNs ต้องลงทะเบียนการทำงานที่กำหนดเองก่อนจึงจะใช้งานได้
ดูข้อมูลเพิ่มเติมได้ในคู่มือการเขียนโปรแกรมการแจ้งเตือนในเครื่องและระยะไกลของ Apple
ดูข้อมูลเชิงลึกเกี่ยวกับการนำส่งข้อความไปยังแอปได้ที่ FCMแดชบอร์ดการรายงาน ซึ่งบันทึก จำนวนข้อความที่ส่งและเปิดในอุปกรณ์ Apple และ Android พร้อมกับ ข้อมูลสำหรับ "การแสดงผล" (การแจ้งเตือนที่ผู้ใช้เห็น) สำหรับแอป Android
จัดการข้อความ Push แบบปิดเสียง
เมื่อส่งข้อความด้วยคีย์ content-available
(เทียบเท่ากับ content-available
ของ APNs) ระบบจะส่งข้อความเป็นการแจ้งเตือนแบบเงียบ
ซึ่งจะเปิดแอปในเบื้องหลังสำหรับงานต่างๆ เช่น การรีเฟรชข้อมูลในเบื้องหลัง
การแจ้งเตือนเหล่านี้ต้องได้รับการจัดการผ่านเมธอด
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
ซึ่งแตกต่างจากการแจ้งเตือนที่ทำงานในเบื้องหน้า
ใช้ application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
ดังที่แสดง
Swift
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 }
Objective-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 เกี่ยวกับ การพุชการอัปเดตในเบื้องหลังไปยังแอป
การตีความเพย์โหลดของข้อความแจ้งเตือน
เพย์โหลดของข้อความการแจ้งเตือนคือพจนานุกรมของ คีย์และค่า ข้อความแจ้งเตือนที่ส่งผ่าน APNs จะเป็นไปตามรูปแบบเพย์โหลดของ APNs ดังนี้
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
จัดการข้อความโดยปิดใช้การสลับเมธอด
โดยค่าเริ่มต้น หากคุณกำหนดคลาสตัวแทนแอปของแอปให้กับพร็อพเพอร์ตี้ตัวแทน UNUserNotificationCenter
และ Messaging
FCM
จะสลับคลาสตัวแทนแอปเพื่อเชื่อมโยงโทเค็น FCM
กับโทเค็น APNs ของอุปกรณ์โดยอัตโนมัติ และส่งเหตุการณ์การแจ้งเตือนที่ได้รับไปยัง Analytics หากปิดใช้การสลับเมธอดอย่างชัดเจน หากคุณ
สร้างแอป SwiftUI หรือหากใช้คลาสแยกต่างหากสำหรับผู้มอบสิทธิ์อย่างใดอย่างหนึ่ง คุณ
จะต้องดำเนินการทั้ง 2 งานนี้ด้วยตนเอง
หากต้องการเชื่อมโยงโทเค็น FCM กับโทเค็น APNs ของอุปกรณ์ ให้ส่งโทเค็น APNs ไปยังคลาส Messaging
ในตัวแฮนเดิลการรีเฟรชโทเค็นของตัวแทนแอป
ผ่านพร็อพเพอร์ตี้ apnsToken
Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken; }
Objective-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) }
Objective-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); }