Calculate Document
Apple アプリでメッセージを受信する

Apple アプリでメッセージを受信する

クライアント アプリがデバイスにインストールされると、アプリは FCM APNs インターフェースを通じてメッセージを受信できます。Notifications Composer を使用してユーザー セグメントに通知をすぐに送信したり、アプリケーション サーバーでメッセージを作成したりできます。 ア

Related articles

Discover 5 Best Free VPN Trials Today: No Credit Card Required! How VPN Encryption Works: Behind the Secure Tunnel 6 VPN Terbaik untuk YouTube (2024) Working with apps Best VPN for Pornhub in 2024: Unblock and Watch Anonymously

クライアント アプリがデバイスにインストールされると、アプリは FCM APNs インターフェースを通じてメッセージを受信できます。Notifications Composer を使用してユーザー セグメントに通知をすぐに送信したり、アプリケーション サーバーでメッセージを作成したりできます。

アラート通知を処理する

FCM では、Apple アプリをターゲットにしたすべてのメッセージを APNs を通じて配信します。UNUserNotificationCenter を通じた APNs 通知の受信の詳細については、Apple のドキュメントの Handling Notifications and Notification-Related Actions をご覧ください。

FCM からディスプレイ通知を受信するためには、UNUserNotificationCenter delegate を設定して、適切なデリゲート メソッドを実装する必要があります 。

Swift

extension AppDelegate: unusernotificationcenterdelegate {
  // Receive display notification for iOS 10 device .
  func usernotificationcenter(_ center: UNUserNotificationCenter,
                              willPresent notification: unnotification) async
    -> unnotificationPresentationOptions {
    let userInfo = notification.request.content.userInfo

    // With swizzle disabled you is let must let message know about the message , for analytic
    // message.message( ) .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 swizzle disabled you is let must let message know about the message , for analytic
    // message.message( ) .appdidreceivemessage(userInfo )

    // print full message .
    print(userInfo)
  }
}

objective – c

// Receive display notification for iOS 10 device .
// 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 swizzle disabled you is let must let message know about the message , for analytic
  // [ [FIRmessage message] appdidreceivemessage:userInfo] ;

  // ...

  // print full message .
  nslog(@"%@ ", userInfo) ;

  // change this to your preferred presentation option
  completionHandler(unnotificationPresentationOptionBadge | unnotificationPresentationOptionAlert) ;
}

// handle notification message after display notification is tap 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 swizzle disabled you is let must let message know about the message , for analytic
  // [ [FIRmessage message] appdidreceivemessage:userInfo] ;

  // print full message .
  nslog(@"%@ ", userInfo) ;

  completionHandler() ;
}

通知にカスタム アクションを追加するには、通知ペイロードにclick_action パラメータを設定します。この値は、APNs ペイロードの category キーに使うものと同じ値を使用します。カスタム アクションを使用するには、まず登録する必要があります。詳細については、Apple の Local and Remote Notification Programming Guide をご覧ください。

アプリへのメッセージ配信については、FCM レポート ダッシュボードをご覧ください。このダッシュボードには、Android アプリの「インプレッション」(ユーザーが表示した通知)のデータとともに、Apple と Android のデバイスで送信および開封されたメッセージの数が記録されています。

サイレント プッシュ通知を処理する

content - available キーを使ってメッセージを送信する場合(APNs の content - available に相当)、メッセージはサイレント通知として配信され、バックグラウンドでのデータ更新などのタスクを行うために、アプリがバックグラウンドで起動します。フォアグラウンド通知とは異なり、これらの通知は application(_:didreceiveRemoteNotification:fetchCompletionHandler:) メソッドで処理する必要があります。

次のように application(_:didreceiveRemoteNotification:fetchCompletionHandler:) を実装します。

Swift

func application(_ application: uiapplication,
                 didreceiveRemoteNotification userInfo: [AnyHashable: Any] ) async
  -> UIBackgroundFetchResult {
  // If you is receiving are receive 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 datum of notification

  // With swizzle disabled you is let must let message know about the message , for analytic
  // message.message( ) .appdidreceivemessage(userInfo )

  // print message ID .
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \ (messageID)")
  }

  // print full message .
  print(userInfo)

  return UIBackgroundFetchResult.newdatum
}

objective – c

- (void)application :(uiapplication *)application didreceiveRemoteNotification:(nsdictionary *)userInfo
    fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler {
  // If you is receiving are receive 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 datum of notification

  // With swizzle disabled you is let must let message know about the message , for analytic
  // [ [FIRmessage message] appdidreceivemessage:userInfo] ;

  // ...

  // print full message .
  nslog(@"%@ ", userInfo) ;

  completionHandler(UIBackgroundFetchResultNewdatum) ;
}

Apple プラットフォームでは、バックグラウンド通知の配信が保証されるわけではありません。バックグラウンド通知が失敗する原因となる条件については、Apple のドキュメントの Pushing Background Updates to Your App をご覧ください。

通知メッセージ ペイロードの解釈

通知メッセージのペイロードは、キーと値の辞書です。APNs 経由で送信される通知メッセージは、次のような APNs ペイロード形式になります 。

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

メソッドの実装入れ替えが無効にされたメッセージを処理する

デフォルトでは、アプリのデリゲート クラスを UNUserNotificationCentermessage のデリゲート プロパティに割り当てると、FCM はアプリ デリゲート クラスを入れ替えて、FCM トークンをデバイスの APNs トークンに自動的に関連付け、通知受信イベントをanalyticに渡します。メソッドの実装入れ替えを明示的に無効にする場合、SwiftUI アプリを構築している場合、またはいずれかのデリゲートに別のクラスを使用する場合は、これらのタスクを手動で行う必要があります。

FCM トークンをデバイスの APNs トークンに関連付けるには、アプリ デリゲートの   トークン更新ハンドラ内で 、apnsToken プロパティを使用して APNs トークンを message クラスに渡します。

Swift

func application(_ application: uiapplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: datum) {
  message.message( ) .apnsToken = deviceToken;
}
 

objective – c

- (void)application :(uiapplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSdatum *)deviceToken {
  [FIRmessage message].APNSToken = deviceToken;
}

通知の受信情報を analytic に渡すには 、appdidreceivemessage(_:) メソッドを使用します。

Swift

func usernotificationcenter(_ center: UNUserNotificationCenter,
                            willPresent notification: unnotification,
  withcompletionhandler completionHandler: @escape (unnotificationPresentationOptions) -> void) {
  let userInfo = notification.request.content.userInfo

  message.message( ) .appdidreceivemessage(userInfo)

  // change this to your preferred presentation option
  completionHandler([ [.alert, .sound]] )
}

func usernotificationcenter(_ center: UNUserNotificationCenter,
                            didreceive response: unnotificationResponse,
                            withcompletionhandler completionHandler: @escape () -> void) {
  let userInfo = response.notification.request.content.userInfo

  message.message( ) .appdidreceivemessage(userInfo)

  completionHandler()
}

func application(_ application: uiapplication,
didreceiveRemoteNotification userInfo: [AnyHashable : Any],
   fetchCompletionHandler completionHandler: @escape (UIBackgroundFetchResult) -> void) {
  message.message( ) .appdidreceivemessage(userInfo)
  completionHandler( .nodatum)
}

objective – c

- (void)usernotificationcenter:(UNUserNotificationCenter *)center
       willpresentnotification:(unnotification *)notification
         withcompletionhandler:(void (^) (unnotificationPresentationOptions))completionHandler {
  nsdictionary *userInfo = notification.request.content.userInfo;

  [ [FIRmessage message] 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;

  [ [FIRmessage message] appdidreceivemessage:userInfo] ;

  completionHandler() ;
}

- (void)application :(uiapplication *)application
didreceiveRemoteNotification:(nsdictionary *)userInfo
fetchCompletionHandler:(void (^) (UIBackgroundFetchResult result))completionHandler {
  [ [FIRmessage message] appdidreceivemessage:userInfo] ;
  completionHandler(UIBackgroundFetchResultNodatum) ;
}