İstemci uygulamanız bir cihaza yüklendikten sonra FCM APNs arayüzü üzerinden mesaj alabilir. Bildirim oluşturucu ile kullanıcı segmentlerine veya uygulama sunucunuzda oluşturulan mesajlara hemen bildirim göndermeye başlayabilirsiniz.
Uyarı bildirimlerini işleme
FCM, Apple uygulamalarını hedefleyen tüm mesajları APNs üzerinden iletir. UNUserNotificationCenter üzerinden APNs bildirimleri alma hakkında daha fazla bilgi edinmek için Apple'ın Bildirimleri ve Bildirimle İlgili İşlemleri Yönetme konusundaki dokümanlarına bakın.
FCM'dan görüntüleme bildirimleri almak için UNUserNotificationCenter delegate'i ayarlamanız ve uygun temsilci yöntemlerini uygulamanız gerekir.
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(); }
Bildirimlerinize özel işlemler eklemek istiyorsanız bildirim yükünde click_action
parametresini ayarlayın.
APNs yükünde category
anahtarı için kullanacağınız değeri kullanın. Özel işlemlerin kullanılabilmesi için öncelikle kaydedilmesi gerekir. Daha fazla bilgi için Apple'ın Yerel ve Uzak Bildirim Programlama Kılavuzu'na bakın.
Uygulamanıza mesaj teslimiyle ilgili analizler için FCM raporlama kontrol paneline bakın. Bu kontrol paneli, Apple ve Android cihazlarda gönderilen ve açılan mesajların sayısını kaydeder. Ayrıca Android uygulamaları için "gösterim" (kullanıcılar tarafından görülen bildirimler) verilerini de içerir.
Sessiz push bildirimlerini işleme
content-available
anahtarıyla (APNs'nin content-available
anahtarına eşdeğer) mesaj gönderirken mesajlar sessiz bildirim olarak teslim edilir ve arka planda veri yenileme gibi görevler için uygulamanız arka planda uyandırılır.
Ön plan bildirimlerinin aksine, bu bildirimler application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
yöntemiyle işlenmelidir.
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
işaretlemesini gösterildiği gibi uygulayın:
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 platformları, arka plan bildirimlerinin teslimini garanti etmez. Arka plan bildirimlerinin başarısız olmasına neden olabilecek koşullar hakkında bilgi edinmek için Apple'ın Uygulamanıza Arka Plan Güncellemeleri Gönderme ile ilgili dokümanlarına bakın.
Bildirim mesajı yükünü yorumlama
Bildirim mesajlarının yükü, anahtar ve değerlerden oluşan bir sözlüktür. APNs üzerinden gönderilen bildirim mesajları, aşağıdaki gibi APNs yük biçimini kullanır:
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
Method swizzling devre dışıyken iletileri işleme
Varsayılan olarak, uygulamanızın uygulama temsilcisi sınıfını UNUserNotificationCenter
ve Messaging
temsilci özelliklerine atarsanız FCM, FCM jetonunuzu cihazın APNs jetonuyla otomatik olarak ilişkilendirmek ve bildirim alındı etkinliklerini Analytics'ye iletmek için uygulama temsilcisi sınıfınızı karıştırır. Method swizzling'i açıkça devre dışı bırakırsanız, SwiftUI uygulaması oluşturuyorsanız veya her iki temsilci için de ayrı bir sınıf kullanıyorsanız bu görevlerin her ikisini de manuel olarak gerçekleştirmeniz gerekir.
FCM jetonunu cihaz APNS jetonuyla ilişkilendirmek için APNS jetonunu, uygulama temsilcinizin jeton yenileme işleyicisindeki Messaging
sınıfına apnsToken
özelliği aracılığıyla iletin.
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; }
Bildirim alındı bilgisi Analytics'ya iletmek için appDidReceiveMessage(_:)
yöntemini kullanın.
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); }