转载自: https://www.jianshu.com/p/f79bb2c3bf7b
//iOS9消息格式如下
//{
// aps = {
// alert = "\U65b0\U6d88\U606f\U6765\U4e86\U3002";
// sound = default;
// };
// d = us60289150408047357911;
// p = 0;
// pustId = 45267;
// pustType = 5;
//}
iOS10消息格式如下
//{
// aps = {
// alert = {
// body = sdfsfsdf;
// subtitle = xxxx;
// title = xxx;
// };
// sound = default;
// };
// d = us29253150408072030511;
// p = 0;
// pustId = 45267;
// pustType = 5;
//}
要根据具体数据格式进行解析,以适配iOS10+。
前台接收消息即app处于打开正在运行的时候,此时,app收到远程推送时候
在iOS9系统下调用
- (void)application:(UIApplication *)application didReceiveRemoteNotification
:(NSDictionary *)userInfo
iOS10系统下调用
//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
}else{
//应用处于前台时的本地推送接受
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
后台接收消息,指app按下home键进入后台或者设备进入锁定状态,此时接收到远程推送消息
iOS9系统下调用:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
iOS10系统下调用:
//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
}else{
//应用处于后台时的本地推送接受
}
}
在iOS9系统下,app接收远程推送消息调用Appdelegate中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
其中launchOptions
中可以获取推送消息内容,然后根据消息内容进行处理。
在iOS10系统下,app接收远程推送消息分为两步
//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
//必须加这句代码
}else{
//应用处于后台时的本地推送接受
}
}
2.然后走AppDelegate中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
其中,launchOption中包含推送消息内容。
iOS9和iOS10由于收到消息内容格式不一样需要分开区别处理。
在iOS9系统下:app启动情况(包括前台和后台)都在
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
中处理推送消息,在app未启动情况下,在AppDelegate中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
处理远程消息。
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
方法中处理推送消息。
在后台或者未启动收到远程推送消息,在
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
方法中处理推送消息。
userNotificationCenter: didReceiveNotificationResponse: withCompletionHandler:
会处理。普及一下,launchOptions参数:
1.App直接启动,launchOptions
为null
2.从他应用程序通过openURL:启动,则UIApplicationLaunchOptionsURLKey
对应的对象为启动URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey
对应启动的源应用程序的bundle ID (NSString);
3.从本地通知启动,则UIApplicationLaunchOptionsLocalNotificationKey
对应的
是本地通知对象(UILocalNotification
);
4.从远程通知启动,则UIApplicationLaunchOptionsRemoteNotificationKey
对应的远程消息通知信息userInfo(NSDictionary);
5.从点击3D Touch iCon启动,则UIApplicationLaunchOptionsShortcutItemKey
对应的是点击的iCon的信息。
作者:roc_lei
链接:https://www.jianshu.com/p/f79bb2c3bf7b
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。