最近專案需要研究了下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 是個控制器,所以可以自定義自己的控制元件,如載入網路圖片,音訊,視訊,webview 等。如果有興趣可以看看demo,demo地址
- 特別注意 先執行工程預設 target ,再執行ALNoti 這個target 選擇之前執行的target
四、執行效果圖
五、推薦文件
- https://www.jianshu.com/p/2f3202b5e758