Panduan ini menjelaskan cara menyiapkan Firebase Cloud Messaging di aplikasi klien seluler dan web Anda sehingga Anda dapat menerima pesan dengan andal.
Setelah diinstal di perangkat, aplikasi klien dapat menerima pesan melalui antarmuka APNs FCM. Anda dapat segera mulai mengirim notifikasi ke segmen pengguna dengan Notifications Composer, atau pesan yang di-build di server aplikasi Anda.
Menangani notifikasi pemberitahuan
FCM mengirim semua pesan yang menargetkan aplikasi Apple melalui APNs. Untuk mempelajari
lebih lanjut cara menerima notifikasi APN menggunakan UNUserNotificationCenter
, lihat
dokumentasi Apple tentang
Handling Notifications and Notification-Related Actions.
Anda harus menyetel delegasi UNUserNotificationCenter dan menerapkan metode delegasi yang sesuai untuk menerima notifikasi tampilan dari 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();
}
Jika Anda ingin menambahkan tindakan kustom ke notifikasi, setel parameter click_action
di
payload notifikasi.
Gunakan nilai yang akan Anda gunakan untuk kunci category
dalam payload APN.
Tindakan kustom harus terdaftar sebelum dapat digunakan. Untuk informasi
selengkapnya, lihat
Local and Remote Notification Programming Guide Apple.
Untuk melihat data terkait pengiriman pesan ke aplikasi Anda, lihat dasbor pelaporan FCM, yang mencatat jumlah pesan yang terkirim dan dibuka di perangkat Apple dan Android, beserta data untuk "tayangan" (notifikasi yang dilihat oleh pengguna) untuk aplikasi Android.
Menangani notifikasi push senyap
Saat mengirim pesan dengan kunci content-available
(setara dengan
content-available
APN), pesan akan dikirim sebagai notifikasi senyap.
Hal ini akan mengaktifkan aplikasi Anda di latar belakang untuk tugas-tugas seperti pemuatan ulang data latar belakang.
Tidak seperti notifikasi latar depan, notifikasi ini harus ditangani menggunakan
metode
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
.
Terapkan application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
seperti yang ditunjukkan berikut:
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);
}
Platform Apple tidak menjamin pengiriman notifikasi latar belakang. Untuk mempelajari kondisi yang dapat menyebabkan notifikasi latar belakang gagal dikirimkan, lihat dokumentasi Apple tentang Pushing Background Updates to Your App.
Menafsirkan payload pesan notifikasi
Payload pesan notifikasi adalah kamus kunci dan nilai. Pesan notifikasi yang dikirim melalui APN memiliki format payload APN berikut ini:
{
"aps" : {
"alert" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
},
"badge" : 1,
},
"customKey" : "customValue"
}
Menangani pesan saat method swizzling nonaktif
Secara default, jika Anda menetapkan class delegasi aplikasi ke properti delegasi UNUserNotificationCenter
dan Messaging
, FCMakan mengganti class delegasi aplikasi untuk secara otomatis mengaitkan token FCM dengan token APNs perangkat dan meneruskan peristiwa yang diterima notifikasi ke Analytics. Jika Anda secara eksplisit menonaktifkan method swizzling, ketika membuat aplikasi SwiftUI atau menggunakan class terpisah untuk salah satu delegasi, Anda perlu melakukan kedua tugas tersebut secara manual.
Untuk mengaitkan token FCM dengan token APN perangkat, teruskan token APN
ke class Messaging
di pengendali pemuatan ulang token
pada delegasi aplikasi Anda
menggunakan
properti 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;
}
Untuk meneruskan informasi penerimaan notifikasi ke Analytics, gunakan
metode 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);
}