Setelah aplikasi klien Anda diinstal di perangkat, aplikasi tersebut dapat menerima pesan melalui antarmuka FCM APN. Anda dapat segera mulai mengirimkan notifikasi ke segmen pengguna dengan Notifications composer , atau pesan yang dibangun di server aplikasi Anda.
Menangani pemberitahuan peringatan
FCM mengirimkan semua pesan yang menargetkan aplikasi Apple melalui APN. Untuk mempelajari lebih lanjut tentang menerima pemberitahuan APN melalui UNUserNotificationCenter, lihat dokumentasi Apple tentang Menangani Pemberitahuan dan Tindakan Terkait Pemberitahuan .
Anda harus menyetel delegasi UNUserNotificationCenter dan mengimplementasikan metode delegasi yang sesuai untuk menerima notifikasi tampilan dari FCM.
Cepat
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(); }
Jika Anda ingin menambahkan tindakan khusus ke notifikasi, atur parameter click_action
di payload notifikasi . Gunakan nilai yang akan Anda gunakan untuk kunci category
di muatan APN. Tindakan kustom harus didaftarkan sebelum dapat digunakan. Untuk informasi lebih lanjut, lihat Panduan Pemrograman Notifikasi Lokal dan Jarak Jauh Apple.
Untuk wawasan tentang pengiriman pesan ke aplikasi Anda, lihat dasbor pelaporan FCM , yang mencatat jumlah pesan yang dikirim dan dibuka di perangkat Apple dan Android, beserta data untuk "tayangan" (pemberitahuan yang dilihat oleh pengguna) untuk aplikasi Android.
Tangani notifikasi push senyap
Saat mengirim pesan dengan kunci content_available
(setara dengan content-available
APN, pesan akan dikirim sebagai pemberitahuan senyap, membangunkan aplikasi Anda di latar belakang untuk tugas seperti penyegaran data latar belakang. Tidak seperti pemberitahuan latar depan, pemberitahuan ini harus ditangani melalui application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
metode.
Terapkan application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
seperti yang ditunjukkan:
Cepat
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); }
Platform Apple tidak menjamin pengiriman pemberitahuan latar belakang. Untuk mempelajari tentang kondisi yang dapat menyebabkan pemberitahuan latar belakang gagal, lihat dokumen Apple tentang Mendorong Pembaruan Latar Belakang ke Aplikasi Anda .
Menafsirkan payload pesan notifikasi
Muatan pesan notifikasi adalah kamus kunci dan nilai. Pesan notifikasi yang dikirim melalui APN mengikuti format muatan APN seperti di bawah ini:
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
Tangani pesan dengan metode swizzling dinonaktifkan
Secara default, jika Anda menetapkan kelas delegasi aplikasi aplikasi Anda ke properti delegasi UNUserNotificationCenter
dan Messaging
, FCM akan mengubah kelas delegasi aplikasi Anda untuk secara otomatis mengaitkan token FCM Anda dengan token APN perangkat dan meneruskan peristiwa yang diterima notifikasi ke Analytics. Jika Anda secara eksplisit menonaktifkan method swizzling, jika Anda membuat aplikasi SwiftUI, atau jika Anda menggunakan kelas terpisah untuk salah satu delegasi, Anda harus melakukan kedua tugas ini secara manual.
Untuk mengaitkan token FCM dengan token APN perangkat, teruskan token APN ke kelas Messaging
di penangan penyegaran token delegasi aplikasi Anda melalui properti apnsToken
.
Cepat
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken; }
Objective-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRMessaging messaging].APNSToken = deviceToken; }
Untuk meneruskan informasi penerimaan notifikasi ke Analytics, gunakan metode appDidReceiveMessage(_:)
.
Cepat
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); }