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 ...
随机推荐
- [bigdata] 启动CM出现 “JDBC Driver class not found: com.mysql.jdbc.Driver” 以及“Error creating bean with name 'serverLogFetcherImpl'”问题的解决方法
问题:“JDBC Driver class not found: com.mysql.jdbc.Driver” 通过以下命令启动cm [root@hadoop1 ~]# /etc/init.d/cl ...
- jQuery-3~4章
jQuery-3~5章 JQuery003-JQuery中的DOM操作 jQuery中的DOM操作: 1.查找节点 A.查找元素节点 B. 查找属性节点 var s1 = $("ul li: ...
- .Net 中的反射(序章) - Part.1
引言 反射是.Net提供给我们的一件强力武器,尽管大多数情况下我们不常用到反射,尽管我们可能也不需要精通它,但对反射的使用作以初步了解在日后的开发中或许会有所帮助. 反射是一个庞大的话题,牵扯到的知识 ...
- win7使用iis并搭建 图片服务器
1.打开控制面板 2.程序-卸载程序 3.点击左边的 打开或关闭windows功能 4.如下图所示,找到internet信息服务勾选.顺便把FTP服务器也全部勾选了,后面会用到 5.进入 控制面板 – ...
- Linux 命令行总结
1.使用ln不加参数,会创建硬链接,如果要创建软连接,需要加-s 参数. # ln test1 test8 -rw-r--r-- root root Nov : test1 -rw-r--r-- ro ...
- JSon 对象转字符的一些方法
引用System.Web.Entity.dll public static string ToJSON(this object obj) { JavaScriptSerializer serializ ...
- bzoj1734 愤怒的牛
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- CSS-dl+dt+dd的应用(非常实用)
http://smallpig301.blog.163.com/blog/static/9986093201010262499229/
- React 还是 Vue: 你应该选择哪一个Web前端框架?
学还是要学的,用的多了,也就有更多的认识了,开发中遇到选择的时候也就简单起来了. 本文作者也做了总结: 如果你喜欢用(或希望能够用)模板搭建应用,请使用Vue 如果你喜欢简单和“能用就行”的东西 ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...