最近项目需要研究了下ios 10 通知新特性,惊奇的发现 通知还能这么玩!
一、环境搭建
创建你的demo 工程并在工程添加new 一个target
二、流程
- 注册通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];//监听回调事件center.delegate = self;//iOS 10 以上使用以下方法注册,才能得到授权[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {// Enable or disable features based on authorization.}];复制代码
- 实现代理
#pragma mark - UNUserNotificationCenterDelegate//通知将要弹出- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { //1. 处理通知//2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式completionHandler(UNNotificationPresentationOptionAlert);}//通知的事件响应的回调- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { NSLog(@" name %@",response.actionIdentifier); completionHandler();}复制代码
- 发送通知
- (void) defaultNoti { UNMutableNotificationContent *noContent = [[UNMutableNotificationContent alloc] init]; noContent.title = _notitle;//标题 noContent.subtitle = @"副标题";//副标题 noContent.body = _content;//正文 noContent.badge = @1;// noContent.launchImageName = @"Icon"; UNNotificationSound *sound = [UNNotificationSound defaultSound]; noContent.sound = sound; noContent.categoryIdentifier = @"asml1"; //1秒后推送通知 UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"default" content:noContent trigger:trigger1]; //通知 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { NSLog(@"%@ error",error); }];}复制代码
- 收到通知处理
- (void)didReceiveNotification:(UNNotification *)notification { NSString *reqID = notification.request.identifier; UNNotificationContent *content = notification.request.content; NSString *url = content.userInfo[@"url"]?:@"http://www.baidu.com"; NSURL *moveURl = [NSURL URLWithString:url]; if ([reqID isEqualToString:@"webView"]) { [self.web loadRequest:[NSURLRequest requestWithURL:moveURl]]; }else if ([reqID isEqualToString:@"video"]) { [self mpPlayerWithUrl:moveURl]; // [self avplayerWithUrl:moveURl]; }else if ([reqID isEqualToString:@"image"]) { UIImageView *imgv = [[UIImageView alloc] initWithFrame:self.view.bounds]; [imgv sd_setImageWithURL:moveURl]; [self.view addSubview:imgv]; }else if ([reqID isEqualToString:@"default"]) { }}复制代码
- 交互事件
//交互事件的获取-(void)didReceiveNotificationResponse:(UNNotificationResponse *)response completionHandler:(void (^)(UNNotificationContentExtensionResponseOption))completion{ //用户点击了切换 if ([response.actionIdentifier isEqualToString:@"asml1"]) { completion(UNNotificationContentExtensionResponseOptionDoNotDismiss); }}复制代码
三、自定义
由于NotificationViewController 是个控制器,所以可以自定义自己的控件,如加载网络图片, 等。如果有兴趣可以看看demo,
- 特别注意 先运行工程默认 target ,再运行ALNoti 这个target 选择之前运行的target
四、运行效果图
五、推荐文档
- https://www.jianshu.com/p/2f3202b5e758