Firebase Messaging - Receive Messages in an iOS App

27 篇文章 0 订阅

转载需要翻墙看的文档,方便查阅。

Receive Messages in an iOS 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 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:

SWIFT
OBJECTIVE-C
 
  
- ( void ) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo {
 
// 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 with  AppDelegate 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.

OBJECTIVE-C: IOS 9
OBJECTIVE-C: IOS 10
SWIFT: IOS 9
SWIFT: IOS 10
 
  
// With "FirebaseAppDelegateProxyEnabled": NO
   
- ( 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.
 
}
 

Since iOS 10, you can set  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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值