From 0bc0bb3c847ce68c10c21a9618bea5cb6b64d048 Mon Sep 17 00:00:00 2001 From: Rizwan Sattar Date: Tue, 23 May 2017 09:27:38 -0700 Subject: [PATCH] Update Messaging samples for new 2.0.0 SDK changes This updates the sample with a couple of issues: 1. It updates the comment about setting the APNs token directly with Messaging instead of InstanceID 2. It adds commented-out lines to show where appDidReceiveMessage: is supposed to be called (similar to the commented-out line about setting the APNs token if swizzling is disabled 3. It adds the iOS 10 delegate handler for data messages, to serve as an example for developers following the app as a template for their own apps --- messaging/MessagingExample/AppDelegate.m | 22 +++++++++++++++++-- .../MessagingExampleSwift/AppDelegate.swift | 22 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/messaging/MessagingExample/AppDelegate.m b/messaging/MessagingExample/AppDelegate.m index ca41e2273..763cb88e3 100644 --- a/messaging/MessagingExample/AppDelegate.m +++ b/messaging/MessagingExample/AppDelegate.m @@ -94,6 +94,9 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N // 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 (userInfo[kGCMMessageIDKey]) { NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); @@ -109,6 +112,9 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N // 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 (userInfo[kGCMMessageIDKey]) { NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); @@ -128,8 +134,12 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { - // Print message ID. NSDictionary *userInfo = notification.request.content.userInfo; + + // With swizzling disabled you must let Messaging know about the message, for Analytics + // [[Messaging messaging] appDidReceiveMessage:userInfo]; + + // Print message ID. if (userInfo[kGCMMessageIDKey]) { NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); } @@ -169,6 +179,14 @@ - (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken: } // [END refresh_token] +// [START ios_10_data_message] +// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground. +// To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES. +- (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage { + NSLog(@"Received data message: %@", remoteMessage.appData); +} +// [END ios_10_data_message] + - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Unable to register for remote notifications: %@", error); } @@ -180,6 +198,6 @@ - (void)application:(UIApplication *)application didRegisterForRemoteNotificatio NSLog(@"APNs device token retrieved: %@", deviceToken); // With swizzling disabled you must set the APNs device token here. - // [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox]; + // [Messaging messaging].APNSToken = deviceToken; } @end diff --git a/messaging/MessagingExampleSwift/AppDelegate.swift b/messaging/MessagingExampleSwift/AppDelegate.swift index d3763a417..4162f9d16 100644 --- a/messaging/MessagingExampleSwift/AppDelegate.swift +++ b/messaging/MessagingExampleSwift/AppDelegate.swift @@ -64,6 +64,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // 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)") @@ -79,6 +82,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // 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)") @@ -97,12 +103,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // This function is added here only for debugging purposes, and can be removed if swizzling is enabled. // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to - // the InstanceID token. + // the FCM registration token. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { print("APNs token retrieved: \(deviceToken)") // With swizzling disabled you must set the APNs token here. - // FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox) + // Messaging.messaging().apnsToken = deviceToken } } @@ -115,6 +121,10 @@ extension AppDelegate : UNUserNotificationCenterDelegate { willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo + + // 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)") @@ -151,5 +161,13 @@ extension AppDelegate : MessagingDelegate { print("Firebase registration token: \(fcmToken)") } // [END refresh_token] + + // [START ios_10_data_message] + // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground. + // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true. + func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { + print("Received data message: \(remoteMessage.appData)") + } + // [END ios_10_data_message] }