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 ...
随机推荐
- python标准模块(三)
本文会涉及到的模块: subprocess logging 1. subprocess 可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 p ...
- 总结的JS数据类型判定(非常全面)
用typeof 来检测数据类型 Javascript自带两套类型:基本数据类型(undefined,string,null,boolean,function,object)和对象类型. 但是如果尝试用 ...
- java.lang.NoClassDefFoundError:org/apache/commons/lang/exception/NestableRuntimeException错误的解决
java.lang.NoClassDefFoundError 是运行时jvm找不到对应类.这种情况是少包的导致的.根据提示语添加对应的jar包就可以. 感叹一下:maven真是一个伟大的东西,在包的依 ...
- ztree.js的使用整理
/** 配置:知识点管理 */ var setting = { view: { showIcon: false, addDiyDom: addPrevDom, addHoverDom: addHove ...
- mysql5.7导入csv文件
环境: Windows10企业版X64 mysql5.7免安装版(从5.6版本开始,官方不再提供64位的msi版本) 运行mysqld.exe启动mysql进程. 用root登录mysql: mysq ...
- 创建线注记LineElement
1.根据2点创建一条线 /// <summary> /// 创建线 /// </summary> /// <param name="pnt1"> ...
- [Unity3d]向量的过度方法以及拖尾效果
Vector3.RotateTowards() 用法 public static function RotateTowards(current: Vector3, target: Vector3, m ...
- 一次性事务和CTE插入数据的比较
有时要构造一些数据来做测试数据,像下面这样: IF OBJECT_ID(N'T14') IS NOT NULL BEGIN DROP TABLE T14 END GO CREATE TABLE T14 ...
- JS 做时钟
今天,给大家分享一个用JS做的时钟. <!DOCTYPE html><html> <head> <meta charset="utf-8" ...
- SQL语句 分页实现
1 通过sql实现分页. select top 5 * from judgeorder where id not in (select top 10 id from judgeorder order ...