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 ...
随机推荐
- 对X86汇编的理解与入门
本文描述基本的32位X86汇编语言的一个子集,其中涉及汇编语言的最核心部分,包括寄存器结构,数据表示,基本的操作指令(包括数据传送指令.逻辑计算指令.算数运算指令),以及函数的调用规则.个人认为:在理 ...
- (转载)KMP算法讲解
网上找到了一篇详细讲解KMP字符串匹配算法,质量很高.特备忘于此. 摘自:http://blog.csdn.net/v_july_v/article/details/7041827 实现代码如下: / ...
- Android hook神器frida(一)
运行环境 ● Python – latest 3.x is highly recommended ● Windows, macOS, or Linux安装方法使用命令 sudo pip install ...
- BootLoader--改进(基于2440)
BootLoader--改进 之前编写的Bootloader启动内核时间使用差不多7秒钟的时间,大多都是用在CPU将内核从Nandflash读取到SDRam中,故首先想到的方法是改变CPU时钟频率. ...
- 第一章:eclipse 中修改字体大小和编码格式
eclipse 中修改字体大小的步骤: 1. 在 eclipse 的工具栏中,找到 weindows 下面的 preferences 2. 在 preferences 的 输出 font ,在 Bas ...
- Java字符串格式化记录
最近打log的时候用到了字符串的格式化. Java中String格式化和C语言的很类似.把情况都列出来,以后好查询. public static void main(String[] args) { ...
- hdu_1695: GCD 【莫比乌斯反演】
题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...
- nodejs 搭建 RESTful API 服务器的常用包及其简介
常用包 框架: yarn add express 数据库链接: yarn add sequelize yarn add mysql2 处理 favicon: yarn add serve-favico ...
- ES6正则表达式扩展
前面的话 正则表达式是javascript操作字符串的一个重要组成部分,但在以往的版本中并未有太多改变.然而,在ES6中,随着字符串操作的变更, ES6也对正则表达式进行了一些更新.本文将详细介绍ES ...
- jmeter之BeanShell对两个变量断言对比
在jmeter的中,断言没法对两个变量的进行对比后判断,只能使用Bean Shell断言来进行. 假设需求: 获取某类型用户uid个数与数据库查询结果是否相等 获取uid个数用http接口获取统计数据 ...