iOS 通知還能這麼玩?

最近項目須要研究了下ios 10 通知新特性,驚奇的發現 通知還能這麼玩!ios

1、環境搭建

建立你的demo 工程並在工程添加new 一個targetgit


2、流程

  • 註冊通知

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);    }

}複製代碼

3、自定義


因爲NotificationViewController 是個控制器,因此能夠自定義本身的控件,如加載網絡圖片,音頻,視頻,webview 等。若是有興趣能夠看看demo,demo地址github

  • 特別注意 先運行工程默認 target ,再運行ALNoti 這個target 選擇以前運行的target 


4、運行效果圖web



5、推薦文檔

  1. https://www.jianshu.com/p/2f3202b5e758
相關文章
相關標籤/搜索