iOS 10推送 - UserNotifications

iOS 10推送 - UserNotifications

字数1633 阅读784 评论0 喜欢9

Collection/Bookmark/Share for width under 768px

一、前言:

在苹果开发者大会WWDC 2016之后,忍不住尝鲜的我,毅然去更新了iOS 10。在刚使用iOS 10的具体感受和意见如何,我们这里就不说了。当然了,既然更新了iOS 10,那Xcode 8也不得不下载了,因为Xcode 7没有办法满足你的真机测试。在今天做项目的时候,刚好用到了推送,然而按照以前推送的写法,编译器居然给我报了个警告, 'UIUserNotificationSettings' is deprecated: first deprecated in iOS 10.0 - Use UserNotifications Framework's UNNotificationSettings。很明显,UIUserNotificationSettings在iOS 10 已经是被弃用了,那么取而代之的,就是UserNotifications。苹果在推送功能增强和改进的同时,也为该新的推送贴上了属于自己的标签(UN),可想苹果对该推送的重视程度了。

二、新的推送的相关注册方法:

  • 在 Appdelegate.m文件中引用推送的头文件
  • #import <UserNotifications/UserNotifications.h>
  • 在didFinishLaunchingWithOptions的方法中,新建我们的通知,详细的代码如下:
    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  •   // 使用 UNUserNotificationCenter 来管理通知
  •   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];  `
  •   // 设置代理为self  
  •   center.delegate = self;  
  •   [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
  •   }]; 
  •   // 注册一个本地的通知  
  • [self registerNotification:2.0];
  •   return YES; 
  • }
  • 在新的推送中,必须要有一个center来管理我们的通知。然后我自定义了一个通知的注册方法,
    - (void)registerNotification:(NSInteger )alerTime。在注册方法里面注册通知,代码如下
    *  注册一个本地的通知  
  • *  
  • *  @param alerTime 延迟通知的时间  
  • */  
  • -(void)registerNotification:(NSInteger )alerTime {  
  •   // 1.使用 UNUserNotificationCenter 来管理通知  
  •   UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];  
  •   // 2.需创建一个包含待通知内容的 UNMutableNotificationContent 对象,注意不是 UNNotificationContent ,此对象为不可变对象。  
  •   UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];  
  •   content.title = @"这是一条通知的标题";  
  •   content.body = @"这是一条通知的主体";  
  •   content.sound = [UNNotificationSound defaultSound];  
  •   // 在 alertTime 后推送本地推送,repeats:是否重复  
  •   UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger  
  •                                                 triggerWithTimeInterval:alerTime repeats:YES];  
  •   // 新建一个推送请求  
  •   UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"Notification"  
  •                                                                         content:content trigger:trigger];  
  •   //添加推送成功后,弹出提示框  
  •   [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {  
  •       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"本地通知" message:@"成功添加推送" preferredStyle:UIAlertControllerStyleAlert];  
  •       UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];  
  •       [alert addAction:cancelAction];  
  •       [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];  
  •   }];
  • 这里需要说明的是,新的推送和之前的推送不同,iOS 10 的推送分为标题、副标题和主体。所以,在代码中,我们都要去设置相对应的属性,当前也可以为空,这样就不会显示出来。上面代码创建的通知,只会显示一次,如果想让通知一直显示的话,可以将repeats这个参数设置为YES,就是重复推送。
    最后,就是在center里添加通知的请求,最后还可以设置,推送成功之后的回调等。
  • 全新的推送有两个代理方法,分别是
    -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
  • -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
    感兴趣的小伙伴可以自己试一下这两个代理方法的具体用法,我这里就不多介绍了。
  • 最后,删除计划推送用下面的方法
    -(void)removePendingNotificationRequestsWithIdentifiers:(NSArray<NSString *> *)identifiers;
    只有一个identifiers参数,这个参数就是之前我们新建推送请求的 identifiers,不过方法要传的是一个数据,我们只需要写一个数据,然后将identifiers放进去就可以了,方法的调用如下:
    [center removePendingNotificationRequestsWithIdentifiers:@[@"Notification"]];

三、这里介绍一下三个Trigger:

分别是:

UNTimeIntervalNotificationTrigger、UNCalendarNotificationTrigger、UNLocationNotificationTrigger

  • UNTimeIntervalNotificationTrigger:概括来说,就是在一个指定的时间内,发起推送。可指定一个时间后发起推送,可指定相隔一段时间发起推送。时间由timeInterval决定,是否重复推送由repeats决定。
  • UNCalendarNotificationTrigger:在指定的一个日期时间发起通知,比如在星期一的8:30发起一个通知,提醒我要去上班。
  • 当然,也可以设置为重复推送,即每周一的8:30发起一个通知。具体发起通知的时间由NSDateComponents类决定,具体的文档,大家可以在Xcode查阅。是否重复推送还是由repeats决定。
  • UNLocationNotificationTrigger:看这个类的命名,就可以猜测是用来当我们在某个地方时,发起通知。没错,这个类,就是当我们进入一个我们规定的区域时,就给我们发起通知。所规定的区域由CLRegion这个类来决定,创建的方法如下:
  • CLRegion *rangedRegions = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:50 identifier:@"rangedRegions"];
  • 传进去的参数有3个,第一个是一个中心,当然这是一个坐标点,需要经纬度,第二个是半径,也就是这个区域的半径,第三个是ID,自定义一个就行。创建好这个区域后,就可以传给CLRegion的创建方法了。是否重复推送还是由repeats决定。意思就是,每次当我进入这个范围的时候,就给我发起一条推送。

应用好这3个Trigger,还是非常好玩的。

  • 用UNCalendarNotificationTrigger就可以做出类似于苹果"提醒事项"的东西了。
  • UNLocationNotificationTrigger也可以做出点好玩的东西,比如,你觉得哪家店的东西很好吃,你就可以将其位置设置为一个范围,当你每次进入该店的一个范围时,手机都会给你推送,“这家店的东西不错。”

四、总结:

关于UserNotifications,远不止上面讲到的,当然还有很多很多。这里就不展开介绍了,大家可以去看下相关文档,文档还是讲的挺详细易懂的。看完之后,你就会发现,苹果对UserNotifications的重视程度真的挺大的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值