Receive messages in an Apple app

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 messages built on your application server.

Handle alert notifications

FCM delivers all messages targeting Apple apps through APNs. To learn more about receiving APNs notifications via UNUserNotificationCenter, see Apple's documentation on Handling Notifications and Notification-Related Actions .

You must set the UNUserNotificationCenter delegate and implement the appropriate delegate methods to receive display notifications from FCM .

Swift

 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 
 (); 
 } 
  

If you want to add custom actions to your notifications, set the click_action parameter in the notification payload . Use the value that you would use for the category key in the APNs payload. Custom actions must be registered before they can be used. For more information, see Apple's Local and Remote Notification Programming Guide .

For insight into message delivery to your app, see the FCM reporting dashboard , which records the number of messages sent and opened on Apple and Android devices, along with data for "impressions" (notifications seen by users) for Android apps.

Handle silent push notifications

When sending messages with the content-available key (equivalent to APNs's content-available ), the messages will be delivered as silent notifications, waking your app in the background for tasks like background data refresh. Unlike foreground notifications, these notifications must be handled via the application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method.

Implement application(_:didReceiveRemoteNotification:fetchCompletionHandler:) as shown:

Swift

 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 platforms don't guarantee the delivery of background notifications. To learn about conditions that can cause background notifications to fail, see Apple's docs on Pushing Background Updates to Your App .

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 messages with method swizzling disabled

By default, if you assign your app's app delegate class to the UNUserNotificationCenter and Messaging delegate properties, FCM will swizzle your app delegate class to automatically associate your FCM token with the device's APNs token and pass notification-received events to Analytics . If you explicitly disable method swizzling, if you are building a SwiftUI app, or if you use a separate class for either delegate, you will need to perform both of these tasks manually.

To associate the FCM token with the device APNs token, pass the APNs token to the Messaging class in your app delegate's token refresh handler via the apnsToken property .

Swift

 func 
  
 application 
 ( 
 _ 
  
 application 
 : 
  
 UIApplication 
 , 
  
 didRegisterForRemoteNotificationsWithDeviceToken 
  
 deviceToken 
 : 
  
 Data 
 ) 
  
 { 
  
 Messaging 
 . 
 messaging 
 (). 
 apnsToken 
  
 = 
  
 deviceToken 
 ; 
 } 
  

Objective-C

 - 
 ( 
 void 
 ) 
 application: 
 ( 
 UIApplication 
  
 * 
 ) 
 application 
  
 didRegisterForRemoteNotificationsWithDeviceToken 
 :( 
 NSData 
  
 * 
 ) 
 deviceToken 
  
 { 
  
 [ 
 FIRMessaging 
  
 messaging 
 ]. 
 APNSToken 
  
 = 
  
 deviceToken 
 ; 
 } 

To pass notification receipt information to Analytics , use the appDidReceiveMessage(_:) method .

Swift

 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 
 ); 
 } 
  
Create a Mobile Website
View Site in Mobile | Classic
Share by: