1、App关闭的情况(Killed),点击消息启动App,首先执行
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
不论iOS9或者iOS10,launchOptions一般就是消息推送的内容。(扯个题外话,如果是点击3D Touch点击iCon进入App,这个launchOptions就是3D Touch的相关数据字典)。如果是iOS10,最好判断下,不处理,因为iOS10的代理方法didReceiveNotificationResponse会处理。
2、App正开着(在后台或者前台)
iOS9, 点击或接收到消息会执行
- (void)application:(UIApplication *)application didReceiveRemoteNotification
:(NSDictionary *)userInfo
可以通过判断 [UIApplication sharedApplication].applicationState 来判断是前台或者后台,
进一步处理跳转或者其他。
iOS10, 点击或接收到消息会执行
处理前台收到通知的代理方法,即App正开启着,用户正在浏览App内容
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSDictionary * userInfo = notification.request.content.userInfo;
NSLog(@"iOS10 前台 userInfo=%@",userInfo);
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
{
//处理远程推送
}
else
{
//处理本地推送
}
}
处理后台点击通知的代理方法,这里的后台和iOS9不一样,指App Killed(即关闭)或者App摁下Home键返回后台的情况
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
NSLog(@"iOS10 后台 userInfo=%@",userInfo);
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
{
//处理远程推送
}
else
{
//处理本地推送
}
}