iOS 10 UserNotification框架解析 – 本地通知
iOS 10
以前的通知比较杂乱,把本地通知和远程通知分开了,诞生了许多功能类似的API,很容易让初学者犯迷糊。而iOS 10
的通知把API
做了统一,利用独立的UserNotifications.framework
框架来管理通知;并且,还增加了撤销单条通知、更新已展示通知、中途修改通知内容等等,以及在通知中展示图片视频,自定义通知UI
等一系列新功能;总之,iOS 10
的通知功能十分强大。
的通知之前,有必要了解一下通知的历史和现状。由于通知可以方便的提示用户应用的状态、传递重要的信息,所以自从iOS 3
引入通知以来,几乎每个新的版本,Apple
都会增强通知的功能。
一、发展历程:
iOS 3 - 引入推送通知
UIApplication 的 registerForRemoteNotificationTypes
与 UIApplicationDelegate
的 application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
,application(_:didReceiveRemoteNotification:)
iOS 4 - 引入本地通知
scheduleLocalNotification
,presentLocalNotificationNow:
, application(_:didReceive:)
iOS 5 - 加入通知中心页面
iOS 6 - 通知中心页面与 iCloud 同步
iOS 7 - 后台静默推送
application(_:didReceiveRemoteNotification:fetchCompletionHandle:)
iOS 8 - 重新设计notification权限请求,加入交互式通知
Actionable 通知 registerUserNotificationSettings(_:),UIUserNotificationAction
与 UIUserNotificationCategory
,application(_:handleActionWithIdentifier:forRemoteNotification:completionHandler:)
iOS 9 - Text Input action
基于 HTTP/2 的推送请求 UIUserNotificationActionBehavior
,全新的 Provider API 等
二、通知应用
申请权限
// 获取通知中心对象
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// 申请通知权限
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"通知开启");
} else {
NSLog(@"关闭通知");
}
}];
// 获取授权的通知权限
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@", settings);
}];
requestAuthorizationWithOptions:
申请通知的权限,iOS 10
不再区分远程通知和本地通知的权限申请,统一用该方法做权限申请
添加通知
// 创建通知对象
UNMutableNotificationContent *noticeContent = [[UNMutableNotificationContent alloc] init]];
// 设置通知标题
noticeContent.title = @"iOS10通知";
// 设置通知子标题
noticeContent.subtitle = @"新通知学习笔记";
// 设置通知内容
noticeContent.body = @"新通知变化很大,之前本地通知和远程推送是两个类,现在合成一个了。";
// 添加通知的声音
UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"];
noticeContent.sound = sound;
UNMutableNotificationContent
通知的内容,可以设置通知的标题、副标题、具体内容,以及通知的图片、视频、声音和交互信息等等
UNNotificationSound
应用在后台时收到通知时的提示音
触发机制
// 如果repeats为YES,那么triggerWithTimeInterval的值要大于60s
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
NSString *requestIdentifier = @"requestIdentifier";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:trigger1];
UNTimeIntervalNotificationTrigger
设置通知触发的条件,分为以下几类:
- 1.
UNPushNotificaitonTrigger
推送服务的Trigger,由系统创建
- 2.
UNTimeIntervalNotificaitonTrigger
时间触发器,可以设置多长时间后触发通知,repeats
参数为是否重复触发,如果设置YES
,那么triggerWithTimeInterval
的时间必须大于60s
- 3.
UNCalendarNotificaitonTrigger
日期触发器,可以设置特定日期触发
- 4.
UNLocationNotificaitonTrigger
位置触发器,用于到达特定位置后才触发通知,需要设置CLRegion
的具体区域
发送通知
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"发送通知出错: %@", error);
}];
addNotificationRequest:withCompletionHandler:
把通知添加到通知中心,就可以发送通知了
扩展通知
我们可以在通知中添加声音、图片和视频,来丰富通知的内容,在支持3D-touch
的手机上,重压之后会全面显示;另外,我们也可以添加交互式通知,进一步提高用户体验
UNNotificationationAttachment
通知的附近,我们可以在附近中添加图片、音频或视频。
添加图片
UNMutableNotificationContent *noticeContent = [[UNMutableNotificationContent alloc] init];
NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"sport" ofType:@"png"];
UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"imageAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];
NSAssert(imageAttachment != nil, @"imageAttach 不能为空");
noticeContent.attachments = @[imageAttachment];
添加视频
UNMutableNotificationContent *noticeContent = [[UNMutableNotificationContent alloc] init];
NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
UNNotificationAttachment *movieAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"movieAttachment" URL:[NSURL fileURLWithPath:movieFile] options:nil error:nil];
NSAssert(movieAttachment != nil, @"movieAttach 不能为空");
noticeContent.attachments = @[movieAttachment];
添加交互式通知
添加交互式通知
UNMutableNotificationContent *noticeContent = [[UNMutableNotificationContent alloc] init];
// 文字action
UNTextInputNotificationAction *action1 = [UNTextInputNotificationAction actionWithIdentifier:@"replyAction" title:@"文字回复" options:UNNotificationActionOptionNone];
// 进入应用action
UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"enterAction" title:@"进入应用" options:UNNotificationActionOptionForeground];
// 取消action
UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"cancelAction" title:@"取消" options:UNNotificationActionOptionDestructive];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"Categroy" actions:@[action1, action2, action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
noticeContent.categoryIdentifier = @"Categroy";
获取交互动作
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
if ([categoryIdentifier isEqualToString:@"Categroy"]) {
if ([response.actionIdentifier isEqualToString:@"replyAction"]) {
UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse*)response;
NSString *userText = textResponse.userText;
NSLog(@"您输入的内容:%@", userText);
} else if ([response.actionIdentifier isEqualToString:@"enterAction"]) {
NSLog(@"点击进入了应用");
} else {
NSLog(@"点击了取消");
}
}
completionHandler();
}
iOS 10
把接收通知的方法做了统一,不再区分接收本地和远程的通知,只分前台和后台时受到的通知。
接收通知的步骤:
1.设置通知的代理,一般设置在AppDelegate
里
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
2.实现代理方法
// 应用在前台收到通知的处理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
// 应用在后台收到通知的处理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
自定义通知样式
iOS 10
可以设置通知的UI
样式,更加方便给用户定制化。必须用iPhone7或iPhone7 Plus,iPhone6s及以下硬件,即使升级到iOS 10
也无法显示自定义通知的样式
1.创建自定义通知UI
文件,在Xcode
中File
->New
->Targe
会出现下面的视图
其中,与通知相关的扩展有两个:Notification Content
和 Notification Service Extension
,前者是自定义通知的类型,后者是我们收到远程通知后,在展示之前对通知的修改。所以,我们采用前者
2.修改通知视图的控制器,自定义通知样式
NotificationViewController
文件:
- (void)didReceiveNotification:(UNNotification *)notification
可以设置UI显示的数据
MainInterface.storyboard
文件
我们可以通过这个storyboard
来具体定制通知的样式
3.应用自定义通知样式
修改NotificationViewController
的info.plist
文件
UNNotificationExtensionCategory
UNNotificationCategory
的标识,需要设置为我们的通知的标识
UNNotificationExtensionInitialContentSizeRatio
通知内容显示的比例大小,默认为1
UNNotificationExtensionDefaultContentHidden
是否隐藏默认通知,设置为YES
后,默认通知会隐藏
移除通知
[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"requestIdentifier"]];
至此,iOS 10
本地通知已经解析完毕,下一篇是远程通知,......
实例项目
https://github.com/BirdandLion/UNNotificationDemo.git
参考资料
https://developer.apple.com/reference/usernotifications
https://onevcat.com/2016/08/notification/
http://www.jianshu.com/p/5713fa2bfece
iOS 10 UserNotification框架解析 – 本地通知的更多相关文章
- [转载]iOS 10 UserNotifications 框架解析
活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- iOS 10 UserNotifications 框架解析
摘自:https://onevcat.com/2016/08/notification/ iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- 10 - 应用程序间通信、本地通知、加速计、URL传输中文
一.应用间通信 URL 调用系统服务: tel:11111 sms:xxx@163.com http:// URL深入 类型://主机:端口/地址?参数 label框等于文字大小快捷键:command ...
- Ios开发中UILocalNotification实现本地通知实现提醒功能
这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time ...
- Spring 10: AspectJ框架 + @Before前置通知
AspectJ框架 概述 AspectJ是一个优秀的面向切面编程的框架,他扩展了java语言,提供了强大的切面实现 本身是java语言开发的,可以对java语言面向切面编程进行无缝扩展 AOP常见术语 ...
- iOS 10 开发问题总结
兼容iOS 10 资料整理笔记 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大 ...
- 开发者所需要知道的 iOS 10 SDK 新特性
转自:https://onevcat.com/2016/06/ios-10-sdk/ 写的很好啊.哈哈哈 总览 距离 iPhone 横空出世已经过去了 9 个年头,iOS 的版本号也跨入了两位数.在我 ...
- 兼容iOS 10 资料整理笔记
原文链接:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化 ...
- iOS开发 - 兼容iOS 10
1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...
随机推荐
- HDOJ 1260 DP
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- Mac之OS系统下搭建JavaEE环境 <二> 之Tomcat 的安装配置
二.Tomcat的安装与配置 1.下载Tomcat 找到Tomcat的官网 百度搜索Tomcat 点击下载即可 下载网址:http://tomcat.apache.org/download-80.cg ...
- Mac终端查看sqlite3数据库、表数据等
背景: 我们在用FMDB处理iOS数据库时,沙盒里保存的数据库格式为.sqlite3. 当我们需要在模拟器上调试或查看数据库内容时,我们可以直接在终端里查看到. 正文: 1.在沙盒路径找到需要查看到文 ...
- 51nod_1119:机器人走方格 V2
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 转化成杨辉三角就好辣@_@ #include< ...
- CSS3浏览器兼容
不同的浏览器需要不同的前缀 -webkit chrome和safari -moz firefox -ms ie -o opera 一个炫酷标题效果: HTML: <!DOCTYPE HTML&g ...
- Certificates does not conform to algorithm constraints
今天在开发时遇到一个新问题:Certificates does not conform to algorithm constraints,在此记录一下解决方案. 问题详情: [ERROR] Faile ...
- 提交到APPStore出现ERROR ITMS-90474
解决的方案是:在工程的targets--->General----->Develoment Info ------->Status BarStyle
- POI读取excel工具类 返回实体bean集合(xls,xlsx通用)
本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <depe ...
- VerilogHDL常用的仿真知识
在描述完电路之后,我们需要进行对代码进行验证,主要是进行功能验证.现在验证大多是基于UVM平台写的systemverilog,然而我并不会sv,不过我会使用verilog进行简单的验证,其实也就是所谓 ...
- 一次浴火重生的MySQL优化(EXPLAIN命令详解)
一直对SQL优化的技能心存无限的向往,之前面试的时候有很多面试官都会来一句,你会优化吗?我说我不太会,这时可能很多人就会有点儿说法了,比如会说不要使用通配符*去检索表.给常常使用的列建立索引.还有创建 ...