不考虑iOS10以下。
- APP在运行中收到通知的处理方法:
执行UIApplicationDelegate代理方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// 1、应用正处在前台状态下,不会收到推送消息,这里创建一个UIAlertController来接受消息
if (application.applicationState == UIApplicationStateActive) {
UIAlertController *alertvc = [UIAlertController alertControllerWithTitle:@"新消息" message:userInfo[@"aps"][@"alert"][@"body"] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* updateAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertvc addAction:cancelAction];
[alertvc addAction:updateAction];
alertvc.modalPresentationStyle = 0;
[self.window.rootViewController presentViewController:alertvc animated:YES completion:nil];
} else if (application.applicationState == UIApplicationStateInactive) {
//2. 处于后台运行状态时
[JPUSHService handleRemoteNotification:userInfo];
//但实际发现并不走这里
}
}
- APP在杀死或在后台运行时收到通知的处理方法:
执行UNUserNotificationCenterDelegate代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
NSString *userAction = response.actionIdentifier;
if ([userAction isEqualToString:UNNotificationDismissActionIdentifier]) {
[JPUSHService resetBadge];
// 用户删除了通知消息,不做操作
}
if ([userAction isEqualToString:UNNotificationDefaultActionIdentifier]) {
//APP在杀死或在后台运行时都会回调这里,在这里可以跳转相应界面
});
}
completionHandler();
}
网友评论