אחרי שאפליקציית הלקוח מותקנת במכשיר, היא יכולה לקבל הודעות דרך ממשק ה-APNs של FCM. אתם יכולים להתחיל לשלוח התראות לפלחי משתמשים באופן מיידי באמצעות הכלי ליצירת התראות, או הודעות שנוצרו בשרת האפליקציות שלכם.
איך מטפלים בהתראות
FCM מעביר את כל ההודעות שמיועדות לאפליקציות של אפל דרך APNs. מידע נוסף על קבלת התראות APNs דרך UNUserNotificationCenter זמין במסמכי התיעוד של Apple בנושא טיפול בהתראות ובפעולות שקשורות להתראות.
כדי לקבל התראות שמוצגות מ-FCM, צריך להגדיר את הנציג של UNUserNotificationCenter ולהטמיע את שיטות הנציג המתאימות.
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
במטען הייעודי (payload) של ההתראה.
משתמשים בערך שבו משתמשים עבור המפתח category
במטען הייעודי (payload) של APNs. כדי להשתמש בפעולות בהתאמה אישית, צריך לרשום אותן קודם. מידע נוסף זמין במדריך לתכנות התראות מקומיות ומרוחקות של 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]) 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 בנושא שליחת עדכונים ברקע לאפליקציה.
הסבר על מטען ייעודי (payload) של הודעה לידיעה
המטען הייעודי (Payload) של הודעות ההתראה הוא מילון של מפתחות וערכים. הודעות ההתראה שנשלחות דרך APNs הן בפורמט המטען הייעודי (payload) של APNs, כמו שמופיע בהמשך:
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
טיפול בהודעות כשהשבתת החלפת שיטות (method swizzling) מושבתת
כברירת מחדל, אם מקצים את מחלקת נציג האפליקציה של האפליקציה למאפייני הנציג UNUserNotificationCenter
ו-Messaging
, FCM יבצע החלפה של מחלקת נציג האפליקציה כדי לשייך באופן אוטומטי את אסימון FCM לאסימון APNs של המכשיר ולהעביר אירועים של קבלת התראות אל Analytics. אם משביתים במפורש את החלפת השיטות, אם בונים אפליקציית SwiftUI או אם משתמשים במחלקה נפרדת לכל אחד מהנציגים, צריך לבצע את שתי המשימות האלה באופן ידני.
כדי לשייך את טוקן FCM לטוקן APNs של המכשיר, מעבירים את טוקן ה-APNs למחלקה Messaging
בtoken refresh handler של delegate האפליקציה באמצעות המאפיין 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); }