iOS10的远程推送
- 注册权限的方法改变了: UNUserNotificationCenter -- requestAuthorizationWithOptions
- 处理(接收)通知的方法变了:
2.1 设置代理 --> 代理有2个新方法
2.2 willPresentNotification: 前台运行 会调用的方法
iOS10之前, 在前台运行时, 不会出现通知的横幅. iOS10会出现横幅
2.3 didReceiveNotificationResponse: 后台运行及程序退出 会调用的方法
本地推送前后台都会调用此方法
2.4 didReceiveRemoteNotification: iOS10只能处理静默推送 (willPresentNotification方法如果不实现, 也可以处理前台)
iOS10之前, 前台/后台/退出/静默推送都可以处理
静默推送 --> iOS7以后出现, 不会出现提醒及声音
可以在收到推送的时候, 自己先执行一段代码, 再提醒用户
- 推送的payload中不能包含alert及sound字段
- 需要添加content-available字段, 并设置值为1
iOS什么时候有了真正的后台多任务: iOS7
静默推送处理完毕, 如果想让用户知道. 你需要自己发送一个本地通知
// 注册推送通知--ios10以前
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 注册推送通知--ios10之后
//iOS10 ,本地推送和远程推送的 注册推送通知的代码一样
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"授权失败%@:", error);
}
else{
NSLog(@"授权成功");
}
}];
center.delegate = self;//为了实现新的代理方法
新的三个代理方法如下:
// 前台运行 会调用的方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"%@",userInfo);
//加lable调试
UILabel *lable = [UILabel new];
lable.backgroundColor = [UIColor redColor];
lable.frame = CGRectMake(50, 50, 300, 300);
lable.text = userInfo.description;
lable.numberOfLines = 0;
[[UIApplication sharedApplication].keyWindow addSubview:lable];
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
//后台运行及程序退出 会调用的方法,
//本地推送前后台都会调用此方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSString *actionStr = response.actionIdentifier;
NSLog(@"actionStr = %@", actionStr);
NSDictionary *userInfo = response.notification.request.content.userInfo;
NSLog(@"%@",userInfo);
//加lable调试
UILabel *lable = [UILabel new];
lable.backgroundColor = [UIColor yellowColor];
lable.frame = CGRectMake(50, 50, 300, 300);
lable.text = userInfo.description;
lable.numberOfLines = 0;
[[UIApplication sharedApplication].keyWindow addSubview:lable];
completionHandler();
}
//iOS10只能处理静默推送 (willPresentNotification方法如果不实现, 也可以处理前台)
//iOS10之前, 前台/后台/退出/静默推送都可以处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
//加lable调试
UILabel *lable = [UILabel new];
lable.backgroundColor = [UIColor greenColor];
lable.frame = CGRectMake(50, 50, 300, 300);
lable.text = userInfo.description;
lable.numberOfLines = 0;
[[UIApplication sharedApplication].keyWindow addSubview:lable];
completionHandler(UIBackgroundFetchResultNewData);
}