Po zainstalowaniu aplikacji klienckiej na urządzeniu może ona odbierać wiadomości przez interfejs FCM APNs. Możesz od razu zacząć wysyłać powiadomienia do segmentów użytkowników za pomocą kompozytora powiadomień lub wiadomości utworzonych na serwerze aplikacji.
Obsługa powiadomień o alertach
FCM dostarcza wszystkie wiadomości kierowane na aplikacje Apple za pomocą APNs. Więcej informacji o otrzymywaniu powiadomień APNs za pomocą UNUserNotificationCenter znajdziesz w dokumentacji Apple na temat obsługi powiadomień i działań z nimi związanych.
Aby otrzymywać powiadomienia wyświetlane z usługi FCM, musisz ustawić delegata UNUserNotificationCenter i wdrożyć odpowiednie metody delegata.
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(); }
Jeśli chcesz dodać do powiadomień działania niestandardowe, ustaw parametr
click_action
w ładunku powiadomienia.
Użyj wartości, której używasz w przypadku klucza category
w ładunku APNs. Zanim będzie można używać działań niestandardowych, należy je zarejestrować. Więcej informacji znajdziesz w przewodniku programowania powiadomień lokalnych i zdalnych firmy Apple.
Aby uzyskać informacje o dostarczaniu wiadomości do aplikacji, otwórz FCM panel raportowania, który rejestruje liczbę wysłanych i otwartych wiadomości na urządzeniach z Androidem i Apple, a także dane o „wyświetleniach” (powiadomieniach widocznych dla użytkowników) w przypadku aplikacji na Androida.
Obsługa cichych powiadomień push
Gdy wysyłasz wiadomości za pomocą klawisza content-available
(odpowiednika klawisza content-available
w usłudze APNs), są one dostarczane jako ciche powiadomienia, które aktywują aplikację w tle w celu wykonania zadań takich jak odświeżanie danych w tle.
W odróżnieniu od powiadomień na pierwszym planie te powiadomienia muszą być obsługiwane za pomocą metody application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
.
Wdróż application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
w sposób pokazany poniżej:
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); }
Platformy Apple nie gwarantują dostarczania powiadomień w tle. Więcej informacji o warunkach, które mogą powodować niepowodzenie powiadomień w tle, znajdziesz w dokumentacji Apple na temat przesyłania aktualizacji w tle do aplikacji.
Interpretowanie ładunku wiadomości z powiadomieniem
Ładunek wiadomości z powiadomieniami to słownik kluczy i wartości. Powiadomienia wysyłane za pomocą APNs mają format ładunku APNs podany poniżej:
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
Obsługa wiadomości przy wyłączonym zamienianiu metod
Domyślnie, jeśli przypiszesz klasę delegata aplikacji do właściwości delegata UNUserNotificationCenter
i Messaging
, FCM zamieni klasę delegata aplikacji, aby automatycznie powiązać token FCM z tokenem APNs urządzenia i przekazywać zdarzenia odebrania powiadomienia do Analytics. Jeśli jawnie wyłączysz zamianę metod, tworzysz aplikację w SwiftUI lub używasz osobnej klasy dla delegata, musisz wykonać obie te czynności ręcznie.
Aby powiązać token FCM z tokenem APNs urządzenia, przekaż token APNs do klasy Messaging
w procedurze obsługi odświeżania tokena delegata aplikacji za pomocą właściwości 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; }
Aby przekazać do Analytics informacje o otrzymaniu powiadomienia, użyj appDidReceiveMessage(_:)
metody.
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); }