iOS10 关于推送
最近在研究iOS10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点:
- 推送内容更加丰富,由之前的alert 到现在的title, subtitle, body
- 推送统一由trigger触发
- 可以为推送增加附件,如图片、音频、视频,这就使推送内容更加丰富多彩
- 可以方便的更新推送内容
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]];//iOS10UNUserNotificationCenter *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 点击打开appUNNotificationAction *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:cenradius:500.0 identifier:@“center"]; region.notifyOnEntry = YES; //进入的时候 region.notifyOnExit = NO; //出去的时候 UNLocationNotificationTrigger *trigger3 = [UNLocationNotificationTriggertriggerWithRegion:region repeats:NO];
添加通知 / 更新通知
- 创建一个UNNotificationRequest类的实例,一定要为它设置identifier, 在后面的查找,更新, 删除通知,这个标识是可以用来区分这个通知与其他通知
- 把request加到UNUserNotificationCenter, 并设置触发器,等待触发
如果另一个request具有和之前request相同的标识,不同的内容, 可以达到更新通知的目的
温馨提示:这个identifier千万不要设置成@”“(空字符串),否则手机就黑屏了,需要重新刷机。亲测属实,不要手贱,具体查看同事发表的文章链接
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);//如果需要在应用在前台也展示通知
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {//点击通知进入应用NSLog(@"response:%@", response);
}
最后
下一篇文章继续介绍关于富媒体推送的 UNNotificationServiceExtension 和 Notification content extension, 未完待续。。。
iOS10 关于推送相关推荐
- iOS10全新推送功能的实现
从iOS8.0开始推送功能的实现在不断改变,功能也在不断增加,iOS10又出来了一个推送插件的开发(见最后图),废话不多说直接上代码: 在开始之前需要打开一个推送开关,不然无法获取deviceToke ...
- iOS10 推送通知 UserNotifications
简介 新框架 获取权限 获取用户设置 注册APNS,获取deviceToken 本地推送流程 远程推送流程 通知策略(Category+Action) 附件通知 代理回调 简介 iOS10新增了Use ...
- iOS10 注册极光推送(干货)
苹果在iOS10上对apns推送做了修改, 极光也是很给力的, 在第一时间就对sdk进行了更新, 下面对iOS10注册极光推送进行一下记录. 首先, 在极光的开发者服务里注册应用获取appKey, 在 ...
- iOS10推送通知(本地远程)/Swift
2019独角兽企业重金招聘Python工程师标准>>> iOS10本地通知 一.发送一个简单的本地通知 1.注册通知 需要导入头文件或者框架UserNotifications 在iO ...
- iOS开发 iOS10推送必看(基础篇)
iOS10更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说.希望看完我的这篇文章,对大家有所帮助. 一.简单入门篇---看完就可以简单适配完了 相对简单的推送证书以及环境的问题,我就不在这 ...
- IOS 10 推送
背景 iOS10 新特性一出,各个大神就早已研究新特性能给场景智能化所带来的好处(唉,可惜我只是一个小白).我也被安排适配iOS10的推送工作! Apple 表示这是 iOS 有史以来最大的升级(ou ...
- iOS 推送(苹果原生)
来自:https://www.jianshu.com/p/3fc46a8764ed 前言 推送对App的重要性不言而喻,是每一个iOS开发者必修的技能.网上的资料对于初学者并不友好(至少对于我来说), ...
- iOS 10 消息推送(UserNotifications)秘籍总结
前言 单独整理消息通知的内容,但是因为工(就)作(是)的(很)事(懒)没有更新文章,违背了自己的学习的初衷.因为互联网一定要有危机意识,说不定眼一睁,我们就欧了 . iOS 10 消息推送(UserN ...
- iOS10 推送必看 UNNotificationContentExtension
来源:徐不同(@2016徐小爷) 链接:http://www.jianshu.com/p/45933f5450a4 大伙久等啦~这绝对是最全最详细的 UNNotificationContentExte ...
- iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐
代码地址如下: http://www.demodashi.com/demo/13208.html 前言 我们首先要在AppDelegate里面进行iOS的适配,可以参考这篇文章 iOS原生推送(APN ...
最新文章
- Spring Boot 集成 Apollo 配置中心,真香、真强大!
- oracle imp 00028,oracle中导入.dmp文件时出现IMP-00009 和IMP-00028异常提示
- qt 使用非系统字库
- rest-framework:频率控制
- 切客软件诞生,给切客全新的购物消费体验
- 无障碍开发(三)之ARIA aria-***属性值
- 实战01_SSM整合ActiveMQ支持多种类型消息
- 北大核心期刊目录2020_2020年RCCSE权威、核心期刊目录总览
- 大一高数求极限的方法小结
- wps表格宏编辑器简单使用
- 迅雷上如何下载热映的电影大片~~
- LKA linux kernel architechture
- LR 类分析方法总结
- C语言lo如何优化运行界面,高质量程序优化总结整理【经典】
- PHP 伪装IP地址 数据采集 GET、POST请求
- Snipe IT资产管理系统(phpstudy_pro)搭建笔记
- 大数据搜索引擎技术_网络数据搜索技术
- 2020阿里云云栖大会奖品活动汇总(持续更新,快收藏)
- 驾考宝典java_驾考宝典java
- Win11系统恢复经典的右键菜单方法