处理远程推送消息的几种情况
app在前台时
在 iOS9 下调用:1
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
在 iOS10 下调用:1
2
3
4
5
6
7
8
9
10-(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在后台时
在 iOS9 下调用:1
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
在 iOS10 下调用:1
2
3
4
5
6
7
8
9//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{
//应用处于后台时的本地推送接受
}
}
app未启动的情况
在 iOS9 下调用:1
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
其中 launchOptions 中可以获取推送消息内容,然后根据消息内容进行处理。
在iOS10系统下,app接收远程推送消息分为两步:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15//先调用
-(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{
//应用处于后台时的本地推送接受
}
}
//然后 AppDelegate 中,launchOption 包含推送消息内容。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
}
最后消息数据格式的区别
1 | //iOS9消息格式 |