Apple uygulamasında mesaj alma

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

Uyarı bildirimlerini yönetin

FCM, Apple uygulamalarını hedefleyen tüm mesajları APN'ler aracılığıyla iletir. UNUserNotificationCenter aracılığıyla APN bildirimleri alma hakkında daha fazla bilgi edinmek için Apple'ın Bildirimleri ve Bildirimle İlgili Eylemleri Ele Alma belgelerine bakın.

FCM'den ekran bildirimleri almak için UNUserNotificationCenter temsilcisini ayarlamanız ve uygun temsilci yöntemlerini uygulamanız gerekir.

Süratli


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)
  }
}

Amaç-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 eylemler eklemek istiyorsanız bildirim verisinde click_action parametresini ayarlayın. APN verisindeki category anahtarı için kullanacağınız değeri kullanın. Özel eylemlerin kullanılmadan önce kaydedilmesi gerekir. Daha fazla bilgi için Apple'ın Yerel ve Uzaktan Bildirim Programlama Kılavuzu'na bakın.

Uygulamanıza mesaj teslimine ilişkin bilgi edinmek için, Apple ve Android cihazlarda gönderilen ve açılan mesajların sayısını ve ayrıca Android uygulamalarına ilişkin "gösterimler" (kullanıcılar tarafından görülen bildirimler) verilerini kaydeden FCM raporlama kontrol paneline bakın.

Sessiz anında bildirimleri yönetin

content_available anahtarıyla (APN'lerin content-available anahtarına eşdeğer) mesaj gönderirken, mesajlar sessiz bildirimler olarak teslim edilecek ve arka plan veri yenileme gibi görevler için uygulamanızı arka planda uyandıracaktır. Ön plan bildirimlerinin aksine, bu bildirimlerin application(_:didReceiveRemoteNotification:fetchCompletionHandler:) yöntemi.

application(_:didReceiveRemoteNotification:fetchCompletionHandler:) öğesini gösterildiği gibi uygulayın:

Süratli

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
}

Amaç-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 iletilmesini garanti etmez. Arka plan bildirimlerinin başarısız olmasına neden olabilecek koşullar hakkında bilgi edinmek için Apple'ın Uygulamanıza Arka Plan Güncellemelerini Aktarma hakkındaki belgelerine bakın.

Bildirim mesajı yükünü yorumlama

Bildirim mesajlarının yükü, anahtarların ve değerlerin bir sözlüğüdür. APN'ler aracılığıyla gönderilen bildirim iletileri, aşağıdaki APN veri yükü biçimini takip eder:

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

Yöntem swizzling devre dışıyken iletileri işleme

Varsayılan olarak, uygulamanızın uygulama temsilci sınıfını UNUserNotificationCenter ve Messaging temsilci özelliklerine atarsanız FCM, FCM belirtecinizi cihazın APN belirteciyle otomatik olarak ilişkilendirmek ve bildirim alınan etkinlikleri Analytics'e iletmek için uygulama temsilci sınıfınızı değiştirir. Yöntem değiştirmeyi açıkça devre dışı bırakırsanız, bir SwiftUI uygulaması oluşturuyorsanız veya her iki temsilci için ayrı bir sınıf kullanıyorsanız, bu görevlerin her ikisini de manuel olarak gerçekleştirmeniz gerekecektir.

FCM belirtecini cihazın APN belirteciyle ilişkilendirmek için, APN belirtecini apnsToken özelliği aracılığıyla uygulama temsilcinizin belirteç yenileme işleyicisindeki Messaging sınıfına iletin.

Süratli

func application(_ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Messaging.messaging().apnsToken = deviceToken;
}
 

Amaç-C

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [FIRMessaging messaging].APNSToken = deviceToken;
}

Bildirim alındı ​​bilgilerini Analytics'e iletmek için appDidReceiveMessage(_:) yöntemini kullanın.

Süratli

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)
}

Amaç-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);
}