Mesajları Apple uygulamasında alın

İstemci uygulamanız bir cihaza yüklendiğinde, FCM APNs arayüzüne gidin. Oturum açma işlemini hemen başlatabilirsiniz. gönderen kullanıcı segmentlerine bildirim göndererek Bildirim oluşturucu veya uygulama sunucusuna geçer.

Uyarı bildirimlerini işleme

FCM, Apple uygulamalarını hedefleyen tüm mesajları APN'ler aracılığıyla gönderir. Daha fazla bilgi edinmek için UNUser NotificationCenter üzerinden APNs bildirimlerini alma hakkında daha fazla bilgi için Apple'ın belgeleri Bildirimleri ve Bildirimle İlgili İşlemleri Ele Alma.

Bu durumda izin verilen UNUser NotificationCenter temsilci ve görüntüleme bildirimlerini almak için uygun yetki verme yöntemlerini uygulayın var.

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ü. APNs yükünde category tuşuna basın. Özel işlemler şu tarihten önce kaydedilmelidir: yardımcı oluyorum. Daha fazla bilgi için Apple'ın Yerel ve Uzaktan Bildirim Programlama Kılavuzu

Uygulamanıza mesaj teslimiyle ilgili analizler için bkz. FCM raporlama kontrol paneli, Apple ve Android cihazlarda gönderilen ve açılan iletilerin sayısı "gösterimler" için veriler (kullanıcılar tarafından görülen bildirimler).

Sessiz push bildirimlerini işleme

content-available tuşuyla mesaj gönderirken (APN'lerin content-available, mesajlar sessiz bildirimler olarak teslim edilecek. arka planda veri yenileme gibi görevler için arka planda uyandırmanızı sağlar. Ön plan bildirimlerinin aksine, bu bildirimler application(_:didReceiveRemoteNotification:fetchCompletionHandler:) yöntemidir.

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

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. Hakkında bilgi edinmek için arka plan bildirimlerinin başarısız olmasına yol açabilen koşullar için Apple'ın dokümanlarına bakın Uygulamanıza Arka Plan Güncellemeleri Aktarma.

Bildirim mesajı yükünü yorumlama

Bildirim mesajlarının yükü bir sözlüktür. anahtarlar ve değerler. APN'ler üzerinden gönderilen bildirim mesajları APN'leri takip eder. yük biçimini şu şekilde değiştirin:

  {
    "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ülkler, FCM uygulamanızı otomatik olarak ilişkilendirmek için Cihazın APNs jetonu ve FCM jetonu ile alınan bildirim Analytics'e aktarmanızı sağlar. Yöntem kaydırmayı açıkça devre dışı bırakırsanız bir SwiftUI uygulaması derlerseniz veya yetki verilmiş kullanıcılar için ayrı bir sınıf kullanıyorsanız bu görevlerin ikisini de manuel olarak gerçekleştirmesi gerekir.

FCM jetonunu cihaz APNs jetonuyla ilişkilendirmek için APN'leri iletin uygulama temsilcinizin Messaging sınıfına jeton jeton yenileme işleyicisi ile apnsToken mülkü.

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