最近在研究iOS10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点:

 1.推送内容更加丰富,由之前的alert 到现在的title, subtitle, body
 2.推送统一由trigger触发
 3.可以为推送增加附件,如图片、音频、视频,这就使推送内容更加丰富多彩
 4.可以方便的更新推送内容 

import 新框架

添加新的框架 UserNotifications.framework

 

#import <UserNotifications/UserNotifications.h> 

注册推送 

在设置通知的时候,需要先进行注册,获取授权
iOS10 所有通知都是通过UNUserNotificationCenter来管理,包括远程通知和本地通知

  //iOS8以下
  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];

  //iOS8 - iOS10
  [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]];

  //iOS10
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {

  }

获取用户设置 

iOS10 提供了获取用户授权相关设置信息的接口getNotificationSettingsWithCompletionHandler: , 回调带有一个UNNotificationSettings对象,它具有以下属性,可以准确获取各种授权信息

authorizationStatus
soundSetting
badgeSetting
alertSetting
notificationCenterSetting
lockScreenSetting
carPlaySetting
alertStyle 

像下面的方法,点击allow

   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
     if (granted) {
        //点击允许
        NSLog(@"注册通知成功");
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@", settings);
        }];
      } else {
        //点击不允许
        NSLog(@"注册通知失败");
      }
    }];

打印信息:   *<UNNotificationSettings: 0x174090a90; authorizationStatus: Authorized, notificationCenterSetting: Enabled, soundSetting: Enabled, badgeSetting: Enabled, lockScreenSetting: Enabled, alertSetting: NotSupported, carPlaySetting: Enabled, alertStyle: Banner>* 

注册APNS, 获取token 

iOS10, 注册APNS和获取token的方法还和之前一样
application: didFinishLaunchingWithOptions:调用 registerForRemoteNotifications方法
 [[UIApplication sharedApplication] registerForRemoteNotifications]; 

在代理方法application: didRegisterForRemoteNotificationsWithDeviceToken:中获取token

 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0){
    NSLog(@"deviceToken:%@",deviceToken);
  }

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0){
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error);
  }
 

设置处理通知的action 和 category 

在iOS8以前是没有category这个属性的;
在iOS8注册推送,获取授权的时候,可以一并设置category, 注册的方法直接带有这个参数;
在iOS10, 需要调用一个方法setNotificationCategories:来为管理推送的UNUserNotificationCenter实例设置category, category又可以对应设置action;

 //设置category
//UNNotificationActionOptionAuthenticationRequired 需要解锁
//UNNotificationActionOptionDestructive 显示为红色
//UNNotificationActionOptionForeground  点击打开app

UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"策略1行为1" options:UNNotificationActionOptionForeground];

UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:@"action2" title:@"策略1行为2" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"comment" textInputPlaceholder:@"reply"];

 //UNNotificationCategoryOptionNone
 //UNNotificationCategoryOptionCustomDismissAction 清除通知被触发会走通知的代理方法
 //UNNotificationCategoryOptionAllowInCarPlay    适用于行车模式
UNNotificationCategory *category1 = [UNNotificationCategory categoryWithIdentifier:@"category1" actions:@[action2,action1] minimalActions:@[action2,action1] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"action3" title:@"策略2行为1" options:UNNotificationActionOptionForeground];

UNNotificationAction *action4 = [UNNotificationAction actionWithIdentifier:@"action4" title:@"策略2行为2" options:UNNotificationActionOptionForeground];
UNNotificationCategory *category2 = [UNNotificationCategory categoryWithIdentifier:@"category2" actions:@[action3,action4] minimalActions:@[action3,action4] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];

[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category1,category2, nil]];
 

设置通知内容 

因为iOS10远程通知与本地通知统一起来了,通知内容属性是一致的,不过远程推送就需要在payload进行具体设置了,下面以本地通知为例,介绍关于UNNotificationContent的内容
官网上明确说明了,我们是不能直接创建UNNotificationContent的实例的, 如果我们需要自己去配置内容的各个属性,我们需要用到UNMutableNotificationContent
看一下它的一些属性:
attachments          //附件
badge                //徽标
body                 //推送内容body
categoryIdentifier   //category标识
launchImageName      //点击通知进入应用的启动图
sound               //声音
subtitle            //推送内容子标题
title               //推送内容标题
userInfo           //远程通知内容

UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  content.title = @"Test";
  content.subtitle = @"1234567890";
  content.body = @"Copyright © 2016年 jpush. All rights reserved.";
  content.badge = @1;
  NSError *error = nil;
  NSString *path = [[NSBundle mainBundle] pathForResource:@"718835727" ofType:@"png"];
  UNNotificationAttachment *att = [UNNotificationAttachment attachmentWithIdentifier:@"att1" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
  if (error) {
    NSLog(@"attachment error %@", error);
  }
  content.attachments = @[att];
  content.categoryIdentifier = @"category1”; //这里设置category1, 是与之前设置的category对应
  content.launchImageName = @"1-Eb_0OvtcxJXHZ7-IOoBsaQ";

UNNotificationSound *sound = [UNNotificationSound defaultSound];
content.sound = sound;

 

通知触发器 

UNNotificationTrigger
iOS 10触发器有4种
 •UNPushNotificationTrigger 触发APNS服务,系统自动设置(这是区分本地通知和远程通知的标识)
 •UNTimeIntervalNotificationTrigger 一段时间后触发
 •UNCalendarNotificationTrigger 指定日期触发
 •UNLocationNotificationTrigger 根据位置触发,支持进入某地或者离开某地或者都有

 //十秒后
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];

//每周日早上8:00
NSDateComponents *component = [[NSDateComponents alloc] init];
component.weekday = 1;
component.hour = 8;
UNCalendarNotificationTrigger *trigger2 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:component repeats:YES];

//圆形区域,进入时候进行通知
CLLocationCoordinate2D cen = CLLocationCoordinate2DMake(80.335400, -90.009201);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:cen
                               radius:500.0 identifier:@“center"];
region.notifyOnEntry = YES; //进入的时候
region.notifyOnExit = NO;  //出去的时候
UNLocationNotificationTrigger *trigger3 = [UNLocationNotificationTrigger
  triggerWithRegion:region repeats:NO];
 

添加通知 / 更新通知

 1.创建一个UNNotificationRequest类的实例,一定要为它设置identifier, 在后面的查找,更新, 删除通知,这个标识是可以用来区分这个通知与其他通知
 2.把request加到UNUserNotificationCenter, 并设置触发器,等待触发
 3.
如果另一个request具有和之前request相同的标识,不同的内容, 可以达到更新通知的目的

  NSString *requestIdentifer = @"TestRequest";
  UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger1];
  //把通知加到UNUserNotificationCenter, 到指定触发点会被触发
  [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  }];

 //在另外需要更新通知的地方
UNMutableNotificationContent *newContent = [[UNMutableNotificationContent alloc] init];
newContent.title = @"Update";
newContent.subtitle = @"XXXXXXXXX";
newContent.body = @"Copyright © 2016年 jpush. All rights reserved.";
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"TestRequest" content:newContent trigger:trigger1];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

}];

 

获取和删除通知 

这里通知是有两种状态
 •Pending 等待触发的通知
 •Delivered 已经触发展示在通知中心的通知

 //获取未触发的通知
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
  NSLog(@"pending: %@", requests);
}];

//获取通知中心列表的通知
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
  NSLog(@"Delivered: %@", notifications);
}];

 //清除某一个未触发的通知
 [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"TestRequest1"]];
 //清除某一个通知中心的通知
 [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"TestRequest2"]];
 //对应的删除所有通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];

 delegate 

<UNUserNotificationCenterDelegate> 

iOS10收到通知不再是在application: didReceiveRemoteNotification:方法去处理, iOS10推出新的代理方法,接收和处理各类通知(本地或者远程)

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  //应用在前台收到通知
  NSLog(@"========%@", notification);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  //点击通知进入应用
  NSLog(@"response:%@", response);
}

最后 

下一篇文章继续介绍关于富媒体推送的 UNNotificationServiceExtension 和 Notification content extension, 未完待续。。。

iOS10 推送最新特性研究的更多相关文章

  1. iOS中指纹识别常见问题汇总

    最近在公司做了一个app要使用指纹支付的功能,在实现过程中遇到各种坑,今天小编抽抗给大家总结把遇到问题汇总特此分享到脚本之家平台,需要的朋友参考下

  2. iOS10 适配以及Xcode8配置总结

    这篇文章主要介绍了iOS10 适配以及Xcode8配置总结的相关资料,本文通过图文并茂的形式给大家介绍,非常不错具有参考借鉴价值,需要的朋友可以参考下

  3. iOS10推送教程详解

    这篇文章主要为大家详细介绍了iOS10推送开发教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  4. Laravel使用swoole实现websocket主动消息推送的方法介绍

    这篇文章主要给大家介绍了关于Laravel使用swoole实现websocket主动消息推送的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Laravel具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

  5. iOS实现远程推送原理及过程

    这篇文章主要为大家详细介绍了iOS实现远程推送原理及具体过程,图文结合的方式针对iOS远程推送进行分析,感兴趣的小伙伴们可以参考一下

  6. iOS10通知框架UserNotification理解与应用

    在iOS10系统中,通知被整合进了UserNotification框架,除了使通知的处理脱离了UIApplication,通知功能的相关开发更加结构化与模块化外,还新增开放了许多更加灵活的开发接口,现在,开发者可以为通知定义UI末班,添加媒体附件,需要的朋友可以参考下

  7. iOS10添加本地推送(Local Notification)实例

    这篇文章主要为大家详细介绍了iOS10添加本地推送(Local Notification)实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  8. 针对iOS10新增Api的详细研究

    这篇文章主要针对iOS10新增Api进行详细研究,基于Api层面,着重看一些具体用法所做的笔记,感兴趣的小伙伴们可以参考一下

  9. 详解适配iOS10 的相关权限设置

    在最新版本的iOS10系统中,如果你的项目中访问了隐私数据,比如:相机、相册、录音、定位、联系人等等。涉及到权限问题,本篇文章主要介绍了适配iOS10 的相关权限设置,有兴趣的可以了解一下。

  10. iOS推送之本地通知UILocalNotification

    这篇文章主要为大家详细介绍了iOS本地通知UILocalNotification,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部