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 ...
随机推荐
- peoplesoft function PSTREENODE 通过 deptid 获得部门树 全路径 名称
create or replace function getUnitFullName(deptid in varchar) return varchar2 is r ); c int; n ); m ...
- poj 2299 Ultra-QuickSort 题解
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- ubuntu12.0.4安装启动后无法进入图形操作界面
在VMware10.0.4虚拟机上安装ubuntu12.0.4版本后,启动linux后,无法进入图形界面,但是可以进入字符界面.通过查阅网上资料,有人说是VMware的3D图形加速没有关闭,于是通过查 ...
- 平板点餐软件编程体会---记我的Android编程之路
前言 想开发一个平板点餐系统,研究下陈江根大侠分享的一个很高水准的实例,只是个单机版无实用意义. (如需运行源码请回复联系邮箱) 实现 Mysql 数据库+Tomcat WEb服务器,使用Servle ...
- SSO(单点登录)与旅游年卡
SSO(单点登录)与旅游年卡 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应 ...
- 关于abp中使用的sweetalert对话框组件的confirm确认对话框中的一个坑
今天修改了一个功能,限制删除用户,在删除的时候不满足条件的时候提示用户原因,使用的sweet alert组件. abp框架前端集成了sweet alert 对http请求的error做了全局处理,我在 ...
- 如何给Ionic写一个cordova插件
写一个cordova插件 之前由javaWeb转html5开发,由于面临新技术,遂在适应的过程中极为挣扎,不过还好~,这个过程也极为短暂:现如今面临一些较为复杂的需求还会有一丝丝头痛,却没有一开始那么 ...
- 华为OJ之最长公共子序列
题目描述: 对于两个给定的字符串,给出他们的最长公共子序列. 题目分析: 1,在之前的博文(http://www.cnblogs.com/yonguo123/p/6711360.html)中我们讨论了 ...
- MySQL后台线程的清理工作
后台清理工作:脏页刷盘.undo回收 1.page cleaner thread:刷新脏页 2.purge thread:清空undo页.清理“deleted”page 一.innodb_page_c ...
- (转)Synchronized(对象锁)和Static Synchronized(类锁)的区别
场景:面试的时候经常用得到! 1 综述 Synchronized和Static Synchronized区别 一个是实例锁(锁在某一个实例对象上,如果该类是单例,那么该锁也具有全局锁的概念),一个是全 ...