クライアント アプリがデバイスにインストールされると、FCM APNs インターフェースを介してメッセージを受信できます。 Notifications composerまたはアプリケーション サーバー上に構築されたメッセージを使用して、ユーザー セグメントへの通知の送信をすぐに開始できます。
アラート通知を処理する
FCM は、Apple アプリを対象とするすべてのメッセージを APN 経由で配信します。 UNUserNotificationCenter 経由で APNs 通知を受信する方法の詳細については、通知の処理と通知関連のアクションに関する Apple のドキュメントを参照してください。
FCM から表示通知を受け取るには、 UNUserNotificationCenter デリゲートを設定し、適切なデリゲート メソッドを実装する必要があります。
迅速
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(); }
通知にカスタム アクションを追加する場合は、通知ペイロードでclick_action
パラメータを設定します。 APNs ペイロードのcategory
キーに使用する値を使用します。カスタム アクションは、使用する前に登録する必要があります。詳細については、Apple のLocal and Remote Notification Programming Guide を参照してください。
アプリへのメッセージ配信について詳しくは、 FCM レポート ダッシュボードを参照してください。このダッシュボードには、Apple および Android デバイスで送信および開封されたメッセージの数と、Android アプリの「インプレッション」(ユーザーが表示した通知) のデータが記録されています。
サイレント プッシュ通知を処理する
content_available
キー (APNs のcontent-available
に相当) を使用してメッセージを送信する場合、メッセージはサイレント通知として配信され、バックグラウンド データ更新などのタスクのためにアプリをバックグラウンドで起動します。フォアグラウンド通知とは異なり、これらの通知はapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
メソッド。
次のようにapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
を実装します。
迅速
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 プラットフォームは、バックグラウンド通知の配信を保証しません。バックグラウンド通知が失敗する原因となる条件については、アプリへのバックグラウンド更新のプッシュに関する Apple のドキュメントを参照してください。
通知メッセージのペイロードの解釈
通知メッセージのペイロードは、キーと値のディクショナリです。 APNs 経由で送信される通知メッセージは、次の APNs ペイロード形式に従います。
{ "aps" : { "alert" : { "body" : "great match!", "title" : "Portugal vs. Denmark", }, "badge" : 1, }, "customKey" : "customValue" }
メソッドの入れ替えを無効にしてメッセージを処理する
デフォルトでは、アプリのアプリ デリゲート クラスをUNUserNotificationCenter
およびMessaging
デリゲート プロパティに割り当てると、FCM はアプリ デリゲート クラスをスウィズルして、FCM トークンをデバイスの APNs トークンに自動的に関連付け、通知受信イベントを Analytics に渡します。メソッドの入れ替えを明示的に無効にする場合、SwiftUI アプリを構築する場合、またはいずれかのデリゲートに別のクラスを使用する場合は、これらのタスクの両方を手動で実行する必要があります。
FCM トークンをデバイスの APNs トークンに関連付けるには、 apnsToken
プロパティを介してアプリ デリゲートのトークン更新ハンドラーでMessaging
クラスに APNs トークンを渡します。
迅速
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { Messaging.messaging().apnsToken = deviceToken; }
Objective-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [FIRMessaging messaging].APNSToken = deviceToken; }
通知受信情報を Analytics に渡すには、 appDidReceiveMessage(_:)
メソッドを使用します。
迅速
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); }