Bu kılavuzda, mesajları güvenilir bir şekilde alabilmeniz için mobil ve web istemci uygulamalarınızda Firebase Cloud Messaging'yı nasıl ayarlayacağınız açıklanmaktadır.
İ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 iletilere hemen bildirim göndermeye başlayabilirsiniz.
Uyarı bildirimlerini işleme
FCM, Apple uygulamalarını hedefleyen tüm mesajları APNs üzerinden iletir. UNUserNotificationCenter
kullanarak APNs bildirimleri alma hakkında daha fazla bilgi edinmek için Apple'ın Bildirimleri ve Bildirimle İlgili İşlemleri İşleme konusundaki belgelerine 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 click_action
parametresini bildirim yükünde ayarlayın.
APNs yükünde category
anahtarı için kullanacağınız değeri kullanın.
Özel işlemlerin kullanılabilmesi için kaydedilmesi gerekir. Daha fazla bilgi için Apple'ın Local and Remote Notification Programming Guide (Yerel ve Uzaktan Bildirim Programlama Kılavuzu) başlıklı makalesine 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ın gördüğü bildirimler) verilerini de içerir.
Sessiz push bildirimlerini işleme
content-available
tuşuyla (APNs'nin content-available
tuşuna 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.
Bu bildirimler, ön plan bildirimlerinin aksine application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
yöntemi kullanılarak 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 teslim edilmesini garanti etmez. Arka plan bildirimlerinin başarısız olmasına neden olabilecek koşullar hakkında bilgi edinmek için Apple'ın Pushing Background Updates to Your App (Uygulamanıza Arka Plan Güncellemeleri Gönderme) başlıklı 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 APNs yük biçimine sahiptir:
{
"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 ikisini de manuel olarak gerçekleştirmeniz gerekir.
FCM jetonunu cihaz APNs jetonuyla ilişkilendirmek için APNs jetonunu, apnsToken
özelliğini kullanarak uygulama temsilcinizin jeton yenileme işleyicisindeki Messaging
sınıfına 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);
}