Mesajları Apple uygulamasında alın

İstemci uygulamanız bir cihaza yüklendikten sonra, FCM APNs arayüzü aracılığıyla mesaj alabilir. Bildirim oluşturucu veya uygulama sunucunuzda oluşturulan mesajlar ile kullanıcı segmentlerine hemen bildirim göndermeye başlayabilirsiniz.

Uyarı bildirimlerini işleme

FCM, Apple uygulamalarını hedefleyen tüm mesajları APN'ler aracılığıyla gönderir. UNUserDescriptionCenter aracılığıyla APNs bildirimleri alma hakkında daha fazla bilgi edinmek için Apple'ın Bildirimleri ve Bildirimle İlgili İşlemleri Yönetme hakkındaki belgelerine göz atın.

FCM'den görüntüleme bildirimleri almak için UNUser NotificationCenter yetkiyi ayarlamalı ve uygun yetki verme yöntemlerini uygulamalısınız.

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 önce kaydedilmesi gerekir. Daha fazla bilgi için Apple'ın Yerel ve Uzaktan Bildirim Programlama Kılavuzu'na bakın.

Uygulamanıza mesaj teslimiyle ilgili analizler için FCM raporlama kontrol paneline göz atın. FCM raporlama kontrol paneli, Apple ve Android cihazlarda gönderilen ve açılan mesaj sayısıyla birlikte Android uygulamalarına ilişkin "gösterimler" (kullanıcıların gördüğü bildirimler) verilerini kaydeder.

Sessiz push bildirimlerini işleme

content-available tuşuyla mesaj gönderilirken (APN'lerin content-available anahtarına karşılık gelir) mesajlar sessiz bildirimler olarak iletilir. Böylece uygulamanız arka planda veri yenileme gibi görevler için arka planda uyandırılır. Ön plan bildirimlerinin aksine, bu bildirimler application(_:didReceiveRemoteNotification:fetchCompletionHandler:) yöntemi ile işlenmelidir.

application(_:didReceiveRemoteNotification:fetchCompletionHandler:) öğesini 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 edileceğini garanti etmez. Arka plan bildirimlerinin başarısız olmasına neden olabilecek koşullar hakkında bilgi edinmek için Uygulamanıza Arka Plan Güncellemelerini Aktarma ile ilgili Apple belgelerine bakın.

Bildirim mesajı yükünü yorumlama

Bildirim mesajlarının yükü, anahtarlar ve değerlerden oluşan bir sözlüktür. APN'ler aracılığıyla gönderilen bildirim mesajları, aşağıdaki APNs yük biçimini kullanır:

  {
    "aps" : {
      "alert" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark",
      },
      "badge" : 1,
    },
    "customKey" : "customValue"
  }

Yöntem kaydırma devre dışı olarak mesajları işleme

Varsayılan olarak, uygulamanızın uygulama yetki sınıfını UNUserNotificationCenter ve Messaging yetki verilmiş mülklerine atarsanız FCM, FCM jetonunuzu cihazın APNs jetonuyla otomatik olarak ilişkilendirmek ve bildirimden alınan etkinlikleri Analytics'e iletmek için uygulama yetki sınıfınızı hareket ettirir. Yöntem kaydırmayı açıkça devre dışı bırakırsanız, bir SwiftUI uygulaması oluşturuyorsanız veya yetki verilen her iki kullanıcı 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 APN'leri jetonuyla ilişkilendirmek için APNs jetonunu, apnsToken özelliği aracılığıyla 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 makbuzu bilgilerini Analytics'e 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);
}