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
相关文章
相关标签/搜索