Skip to content

Commit

Permalink
Update Messaging samples for new 2.0.0 SDK changes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsattar committed May 23, 2017
1 parent 6b697a2 commit 0bc0bb3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
22 changes: 20 additions & 2 deletions messaging/MessagingExample/AppDelegate.m
Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
22 changes: 20 additions & 2 deletions messaging/MessagingExampleSwift/AppDelegate.swift
Expand Up @@ -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)")
Expand All @@ -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)")
Expand All @@ -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
}
}

Expand All @@ -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)")
Expand Down Expand Up @@ -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]
}

0 comments on commit 0bc0bb3

Please sign in to comment.