Once your client app is installed on a device, it can receive messages through the FCM APNs interface. You can immediately start sending notifications to user segments with the Notifications composer, or your application server can send messages with a notification payload through the APNs interface.
Handling messages received through the FCM APNs interface is likely to cover most typical use cases. You can also send upstream messages, or receive data messages in foregrounded apps.
Handle messages received through the FCM APNs interface
When your app is in the background, iOS directs messages with the notification
key to the system tray. A tap on a notification opens the app, and the content of the notification is passed to thedidReceiveRemoteNotification
callback in the AppDelegate
.
Implement AppDelegate application:didReceiveRemoteNotification:
as shown:
// 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 message ID.
if ( userInfo [ kGCMMessageIDKey ]) {
NSLog (@ "Message ID: %@" , userInfo [ kGCMMessageIDKey ]);
}
// Print full message.
NSLog (@ "%@" , userInfo );
}
- ( 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 message ID.
if ( userInfo [ kGCMMessageIDKey ]) {
NSLog (@ "Message ID: %@" , userInfo [ kGCMMessageIDKey ]);
}
// Print full message.
NSLog (@ "%@" , userInfo );
completionHandler ( UIBackgroundFetchResultNewData );
}
If you want to open your app and perform a specific action, set click_action
in the notification payload. Use the value that you would use for the category
key in the APNs payload.
Interpreting notification message payload
The payload of notification messages is a dictionary of keys and values. Notification messages sent through APNs follow the APNs payload format as below:
{"aps" : {
"alert" : {
"body" : "great match!" ,
"title" : "Portugal vs. Denmark" ,
},
"badge" : 1 ,
},
"customKey" : "customValue"
}
Handle data messages in foregrounded apps
To receive data-only messages directly from FCM (as opposed to via APNs) when the app is in the foreground, you'll need to connect to the FCM service and handle messages withAppDelegate applicationReceivedRemoteMessage:
.
To connect, set the shouldEstablishDirectChannel
flag to YES
in the AppDelegate
. FCM manages the connection, closing it when your app goes into the background and reopening it whenever the app is foregrounded.
To receive data messages when your app is in the foreground, implement applicationReceivedRemoteMessage:
. Your app can still receive data messages when it is in the background without this callback, but for foreground cases you'll need to handle it.
Handle messages with method swizzling disabled
If you disable method swizzling, you'll need to call a method to let FCM track message delivery and analytics. This is performed automatically with method swizzling enabled.
- ( void ) application :( UIApplication *) application
didReceiveRemoteNotification :( NSDictionary *) userInfo
fetchCompletionHandler :
( void (^)( UIBackgroundFetchResult )) completionHandler {
// Let FCM know about the message for analytics etc.
[[ FIRMessaging messaging ] appDidReceiveMessage : userInfo ];
// handle your message.
}
UNUserNotificationCenter delegate
to receive display notifications from Apple and
FIRMessaging remoteMessageDelegate
to receive data messages from FCM. If you do not set these two delegates with AppDelegate, method swizzling for message handling is disabled. You'll need to call method
appDidReceiveMessage: to track message delivery and analytics.
Interpreting data message payload
The payload of data messages is a dictionary of keys and values. Data messages sent to the devices directly by FCM server are expressed in the format of a dictionary as below:
{"body" : "great match!" ,
"title" : "Portugal vs. Denmark" ,
"icon" : "myicon"
}
Handle queued and deleted messages
Apps that connect to FCM to receive data messages should handleFIRMessagingMessagesDeletedNotification
. You may receive this callback when there are too many messages (>100) pending for your app on a particular device at the time it connects, or if the device hasn't connected to FCM for more than one month. When the app instance receives this callback, it should perform a full sync with your app server.