| Platform seçin: | iOS+ Android Web Flutter Unity C++ |
İstemci uygulamanız bir cihaza yüklendikten sonra FCM APNs arayüzü üzerinden mesaj alabilir. Firebase konsolundaki Bildirim Oluşturucu'yu kullanarak veya uygulama sunucunuzda oluşturulan mesajlarla kullanıcı segmentlerine bildirim göndermeye hemen 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 Bildirimlerle İlgili İşlemleri Yönetme başlıklı dokümanına bakın.
FCM üzerinden bildirim almak için UNUserNotificationCenter temsilcisini ayarlamanız ve uygun temsilci yöntemlerini uygulamanız gerekir.
Swift
// 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
// Note: UNNotificationPresentationOptions.alert has been deprecated.
return [.list, .banner, .sound]
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse) async {
let userInfo = response.notification.request.content.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(UNNotificationPresentationOptionList |
UNNotificationPresentationOptionBanner |
UNNotificationPresentationOptionSound);
}
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
NSLog(@"FCM registration token: %@", fcmToken);
// Notify about received token.
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
[[NSNotificationCenter defaultCenter] postNotificationName:
@"FCMToken" object:nil userInfo:dataDict];
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
- (void)logFCMToken {
NSString *fcmToken = [FIRMessaging messaging].FCMToken;
NSLog(@"Local FCM registration token: %@", fcmToken);
NSString* displayToken = [NSString stringWithFormat:@"Logged FCM token: %@", fcmToken];
[[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error fetching the remote FCM registration token: %@", error);
} else {
NSLog(@"Remote FCM registration token: %@", token);
NSString* message =
[NSString stringWithFormat:@"FCM registration token: %@", token];
// display message
NSLog(@"%@", message);
}
}];
NSLog(@"%@", displayToken);
}
- (void)subsribeToTopic {
[[FIRMessaging messaging] subscribeToTopic:@"weather"
completion:^(NSError * _Nullable error) {
NSLog(@"Subscribed to weather topic");
}];
}
@end
Bildirimlerinize özel işlemler eklemek istiyorsanız notification
payload içinde click_action parametresini ayarlayın.
APNs yükündeki 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 Yerel ve Uzaktan Bildirim Programlama Kılavuzu'na bakın.
Uygulamanıza mesaj teslimi hakkında bilgi edinmek için Firebase konsolunda DevOps ve Etkileşim > Mesajlaşma > Raporlar kontrol paneli'ne gidin. Bu kontrol panelinde, Apple ve Android cihazlarda gönderilen ve açılan mesajların sayısı ile Android uygulamaları için "gösterim" (kullanıcılar tarafından görülen bildirimler) verileri kaydedilir.
Sessiz push bildirimlerini işleme
content-available anahtarıyla (APN'lerin content-available anahtarına eşdeğer) mesaj gönderirken mesajlar sessiz bildirimler olarak teslim edilir ve arka planda veri yenileme gibi görevler için uygulamanızı arka planda uyandırır.
Ön plan bildirimlerinin aksine, bu bildirimler application(_:didReceiveRemoteNotification:fetchCompletionHandler:) yöntemi kullanılarak işlenmelidir.
application(_:didReceiveRemoteNotification:fetchCompletionHandler:) öğesini gösterildiği gibi uygulayın:
Swift
@MainActor
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 full message.
print(userInfo)
print("Call exportDeliveryMetricsToBigQuery() from AppDelegate")
Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: 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 until 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 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"
}
Yöntem dönüşümü devre dışıyken mesajları 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 APN jetonuyla otomatik olarak ilişkilendirmek ve bildirim alındı etkinliklerini Analytics'e iletmek için uygulama temsilcisi sınıfınızı değiştirir. Yöntem dönüşümünü açıkça devre dışı bırakırsanız, bir SwiftUI uygulaması geliştiriyorsanız veya her iki temsilci için 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 iletmek için Analytics, 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);
}