Apple uygulamasında mesaj alma

İstemci uygulamanız bir cihaza yüklendikten sonra, FCM APNs arabirimi aracılığıyla iletileri alabilir. Notifications bestecisi ile kullanıcı segmentlerine anında bildirim göndermeye veya uygulama sunucunuz üzerinde oluşturulmuş mesajlar göndermeye başlayabilirsiniz.

Uyarı bildirimlerini işleme

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 İşleme hakkındaki belgelerine bakın.

UNUserNotificationCenter temsilcisini ayarlamanız ve FCM'den ekran bildirimleri almak için 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 yükünde click_action parametresini ayarlayın. APN'ler yükünde category anahtarı için kullanacağınız değeri kullanın. Kullanılmadan önce özel eylemlerin kaydedilmesi gerekir. Daha fazla bilgi için, Apple'ın Yerel ve Uzaktan Bildirim Programlama Kılavuzu'na bakın.

Uygulamanıza ileti teslimi hakkında fikir edinmek için, Android uygulamaları için "gösterimler" (kullanıcılar tarafından görülen bildirimler) verileriyle birlikte Apple ve Android cihazlarda gönderilen ve açılan iletilerin sayısını kaydeden FCM raporlama panosuna bakın.

Sessiz push bildirimlerini yönetin

content_available anahtarıyla mesaj gönderirken (APN'lerin content-available anahtarına eşdeğerdir), mesajlar sessiz bildirimler olarak teslim edilir ve uygulamanızı arka planda veri yenileme gibi görevler için uyandırır. Ön plan bildirimlerinden farklı olarak, bu bildirimler 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 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 Uygulamanıza Arka Plan Güncellemelerini Aktarma hakkındaki belgelerine bakın.

Bildirim mesajı yükünü yorumlama

Bildirim mesajlarının yükü, bir anahtarlar ve değerler sözlüğüdür. APN'ler aracılığıyla gönderilen bildirim mesajları, aşağıdaki gibi APN'lerin veri yükü formatını 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 APNs belirteciyle otomatik olarak ilişkilendirmek ve bildirim alınan olayları Analytics'e iletmek için uygulama temsilci sınıfınızı değiştirir. Yöntem swizzling'i 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 gerekir.

FCM belirtecini cihaz APNs belirteciyle ilişkilendirmek için, APNs 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ıcı 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);
}