ios10新特性-UserNotification
引言:iOS的通知分本地通知和远程通知,iOS10之前采用的是UILocationNotification类,远程通知有苹果服务器进行转发,本地通知和远程通知其回调的处理都是通过AppDelegate中的几个回调方法来完成。iOS10系统中,通知功能的增强是一大优化之处,iOS10中将通知功能整合成了一个框架UserNotification,其结构十分类似于iOS8中的UIWebView向WebKit框架整合的思路。并且UserNotification相比之前的通知功能更加强大,主要表现在如下几点:
1.通知处理代码可以从AppDelegate中剥离。
2.通知的注册,设置,处理更加结构化,更易于模块化开发。
3.UserNotification支持自定义通知音效和启动图。
4.UserNotification支持向通知内容中添加媒体附件,例如音频,视频。
5.UserNotification支持开发者定义多套通知模板。
6.UserNotification支持完全自定义的通知界面。
7.UserNotification支持自定义通知中的用户交互按钮。
8.通知的触发更加容易管理。
从上面列举的几点就可以看出,iOS10中的UsreNotification真的是一个大的改进,温故而知新,关于iOS之前版本本地通知和远程通知的相关内容请查看如下博客:
本地推送:http://my.oschina.net/u/2340880/blog/405491。
远程推送:http://my.oschina.net/u/2340880/blog/413584。
更多详细内容可以参考这篇博客:https://my.oschina.net/u/2340880/blog/747781
demo参考:http://www.open-open.com/lib/view/open1472632538972.html
下面就是我自己写的小程序,小试牛刀一下:
第一步必不可少的肯定是要导入我们的头文件:<UserNotifications/UserNotifications.h>
然后在AppDelegate.m中注册通知(第一次写的时候就是没有注册通知,直接就写了,所以导致通知总是不显示)
#import "AppDelegate.h"
#import "ViewController.h"
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
//注册本地推送
// 使用 UNUserNotificationCenter 来管理通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//监听回调事件
center.delegate = self;
//iOS 10 使用以下方法注册,才能得到授权
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
//获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}];
//
// [self addLocationNotification:5];
return YES;
}
#pragma mark - UNUserNotificationCenterDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
//1. 处理通知
//2. 处理完成后条用 completionHandler ,用于指示在前台显示通知的形式
completionHandler(UNNotificationPresentationOptionAlert);
}
2.然后在ViewController.m文件里发送通知
#import "ViewController.h"
#import <UserNotifications/UserNotifications.h>
@interface ViewController ()
@property (nonatomic, strong) NSString *notitle;//通知标题
@property (nonatomic, strong) NSString *content;//通知内容
@property (nonatomic, strong) NSURL *lineUrl;//跳转链接
@property (nonatomic, strong) NSURL *imageUrl;//附加的图片
@property (nonatomic, strong) NSData *soundData;//声音
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"新通知测试";
self.view.backgroundColor = [UIColor whiteColor];
[self setUpUI];
}
- (void)setUpNotification {
//初始化通知
UNMutableNotificationContent *noContent = [[UNMutableNotificationContent alloc] init];
noContent.title = _notitle;//标题
noContent.subtitle = @"副标题";//副标题
noContent.body = _content;//正文
noContent.badge = @1;//
UNNotificationSound *sound = [UNNotificationSound defaultSound];
noContent.sound = sound;
noContent.categoryIdentifier = @"uid";
//5秒后推送通知
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"push" content:noContent trigger:trigger1];
//通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"%@ error",error);
}];
}
- (void)setUpUI {
_notitle = @"通知标题:iOS10测试";
_content = @"这是一条紧急通知";
_lineUrl = [NSURL URLWithString:@"http://www.cnblogs.com/zrr-notes/"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"发送通知" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor redColor]];
btn.frame = CGRectMake(100, 100, 100, 50);
[self.view addSubview:btn];
[btn addTarget:self action:@selector(setUpNotification) forControlEvents:UIControlEventTouchUpInside];
}
ios10新特性-UserNotification的更多相关文章
- iOS10新特性之CallKit开发详解:锁屏接听和来电识别
国庆节过完了,回家好好休息一天,今天好好分享一下CallKit开发.最近发现好多吃瓜问CallKit的VoIP开发适配,对iOS10的新特性开发和适配也在上个月完成,接下来就分享一下VoIP应用如何使 ...
- iOS10新特性之SiriKit
在6月14日凌晨的WWDC2016大会上,苹果提出iOS10是一次里程碑并且推出了十个新特性,大部分的特性是基于iPhone自身的原生应用的更新,具体的特性笔者不在这里再次叙述,请看客们移步WWDC2 ...
- Xcode8新特性和iOS10新特性
从 Xcode 8.0 开始,目前所有的插件都无法工作! NSLog 无法输出 -- 此bug等待正式版本... Xcode 提供了文档注释快捷键option + cmd + / 但是要把系统升级到1 ...
- iOS10 新特性-新功能,以及ReplayKit库
iOS的10.0 本文总结了iOS的10,运行于目前推出iOS设备推出的主要开发者相关的功能. iOS版10引入了新的方法来增加您的应用程序通过帮助系统参与在适当的时候建议你的应用程序给用户.如果你在 ...
- iOS10 新特性一
链接:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是 ...
- iOS10新特性
1.Siri API 的开放自然是 iOS 10 SDK 中最激动人心也是亮眼的特性.Apple 加入了一套全新的框架 Intents.framework 来表示 Siri 获取并解析的结果. 在 i ...
- WDC2106 iOS10新特性及开发者要注意什么
昨晚苹果在旧金山召开了WWDC,看了WWDC2016直播,我们发现变得谨慎而开放的苹果在新一版四大平台系统中展示了很多变化,当然重中之重还是伟大的iOS.通过试用iOS10beta版,除了长大了的更强 ...
- ios9和ios10的新特性
昨天面试了一个做ios开发的公司,其中面试官问我最新的ios系统版本是多少,以及它的特性是什么?由于自己是初学者,所以对这些没有关注过.今天特地搜索了一下关于ios9和ios10的新特性,并整理了一下 ...
- iOS UICollection 和UITableview新特性
很详细优秀的博客: http://www.jianshu.com/p/e97780a24224 iOS10新特性总结 http://blog.csdn.net/yyacheng/article/det ...
随机推荐
- docker入门记录1
一. 什么是Docker 1.英文意思是集装箱,很形象.直白点就是将程序运行环境打包在一个箱子里,然后箱子扔到哪里,里边的程序都可以运行.这样以来一个显而易见的好处是:和以前的开发环境等相比,你不用每 ...
- POJ3249:Test for Job
传送门 很简单的一道题,被卡了几次,死于答案非法统计. 题意是求图里的一条最长的路径满足起点的入度和终点的出度都是0,而且图是DAG. 既然是DAG求最长路,DP即可.搞出拓扑序,逆序DP,然后统计所 ...
- PHP Ajax 跨域问题最佳解决方案
本文通过设置Access-Control-Allow-Origin来实现跨域. 例如:客户端的域名是client.runoob.com,而请求的域名是server.runoob.com. 如果直接使用 ...
- Yii2.0 用户登录详解(上)
一.准备 在开始编写代码之前,我们需要思考一下:用户登陆模块,实现的是什么功能?很明显,是登陆功能,那么,登陆需要用户名和密码,我们在数据库的一张表中就应该准备好用户名和密码的字段,再思考一下,如果要 ...
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了(转载)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var mysql 启动不了 ps -A | gr ...
- Response.End()出现ThreadAbortException 异常
A. 如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常.异常内容:由于代码已经过 ...
- 【Go入门教程5】面向对象(method、指针作为receiver、method继承、method重写)
前面两章我们介绍了函数和struct,那你是否想过函数当作struct的字段一样来处理呢?今天我们就讲解一下函数的另一种形态,带有接收者(receiver)的函数,我们称为method method ...
- 在浏览器上直接输入url 时,中文传参乱码问题
这样的地址 xxx.asp?name=中国 ,通过 超链接打开这个链接 ,xxx.asp能够成才接收参数,但是如果将地址直接放到浏览器地址栏上,回车, xxx.asp就无法正确接收中文参数,一直显示 ...
- 1.1ASP.NET Web API 2入门
HTTP 不只是为了生成 web 页面.它也是建立公开服务和数据的 Api 的强大平台.HTTP 是简单的. 灵活的和无处不在.你能想到的几乎任何平台有 HTTP 库,因此,HTTP 服务可以达到范围 ...
- Microsoft.AspNet.SignalR 2.2
Nuget :http://www.nuget.org/packages/Microsoft.AspNet.SignalR/ What is SignalR? ASP.NET SignalR is a ...