您的位置:首页 > 移动开发 > IOS开发

iOS10本地通知UserNotifications快速入门

2016-11-28 17:31 543 查看
iOS10更新变动最大的就是通知这部分了,新版通知变得更加统一,使用更加方便,设计更加自由。以前本地通知和远程推送是分开的,虽然这些到了iOS10都合在一起了,但是为了便于理解,我们还是把他俩分开来进行学习。这节我们学习的是本地通知。

以下的用语,如无特别表述,
通知
就代表本地通知,
推送
就代表远程服务器的推送。

快速添加一个通知

我们先举个完整的代码例子,大家了解下这个流程,然后分步介绍这几项:

//第一步:注册通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//请求获取通知权限(角标,声音,弹框)
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//获取用户是否同意开启通知
NSLog(@"request authorization successed!");
}
}];
}
//第二步:新建通知内容对象
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]
content.title = @"iOS10通知";
content.subtitle = @"新通知学习笔记";
content.body = @"新通知变化很大,之前本地通知和远程推送是两个类,现在合成一个了。这是一条测试通知,";
content.badge = @1;
UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"];
content.sound = sound;

//第三步:通知触发机制。(重复提醒,时间间隔要大于60s)
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];

//第四步:创建UNNotificationRequest通知请求对象
NSString *requertIdentifier = @"RequestIdentifier";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];

//第五步:将通知加到通知中心
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error:%@",error);

}];

最终效果如下:



notification01.png

通知内容UNMutableNotificationContent

通知内容就是设定通知的一些展示信息,iOS10之后可以设置subtitle。

声音的设置需要借助一个新类
UNNotificationSound
,通知文件要放到bundle里面。另外在实际的测试过程中发现,添加通知的声音有时候会无效。这应该是iOS10存在的一个bug,删除掉程序,再安装运行就好了。

触发机制UNNotificationTrigger

Trigger是新加入的一个功能,通过此类可设置本地通知触发条件。它一共有一下几种类型:

1、UNPushNotificaitonTrigger

推送服务的Trigger,由系统创建

2、UNTimeIntervalNotificaitonTrigger

时间触发器,可以设置多长时间以后触发,是否重复。如果设置重复,重复时长要大于60s

3、UNCalendarNotificaitonTrigger

日期触发器,可以设置某一日期触发。例如,提醒我每天早上七点起床:

NSDateComponents *components = [[NSDateComponents alloc] init];
components.hour = 7;
components.minute = 0; // components 日期
UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

4、UNLocationNotificaitonTrigger

位置触发器,用于到某一范围之后,触发通知。通过CLRegion设定具体范围。

通知请求UNNotificationRequest

通知请求的构造
+ (instancetype)requestWithIdentifier:(NSString *)identifier content:(UNNotificationContent *)content trigger:(nullable UNNotificationTrigger *)trigger;


就是把上面三项连接起来。它有一个参数identifier,这相当于通知的一个身份。iOS10通知支持更新,就是基于此identifier再发一条通知。

通知中心UNUserNotificationCenter

获取通知
[UNUserNotificationCenter currentNotificationCenter]
然后通过
addNotificaitonRequest:
就完成了一个通知的添加。

扩展通知的内容

通知我们已经添加上了,现在我们需要扩展一下通知的内容,给它加一些内容。扩展的内容需要支持3D-touch的手机(6s以上),重压之后全面显示

添加附件

iOS10之前通知的样式不能更改,在iOS10之后引入了UNNotificationationAttachment,可以在通知中添加图片,音频,视频。苹果对这些附件的大小和类型有一个限制:



attachment_type.png

如果我想在通知里加一个图片,可以这样处理:

NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"sport" ofType:@"png"];
UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];
content.attachments = @[imageAttachment];//虽然是数组,但是添加多个只能显示第一个
/* add request and notificaiton code ... */

效果如下:



notification02.png

重压之后:



notificaiton03.png

添加交互

//点击可以显示文本输入框
UNTextInputNotificationAction *action1 = [UNTextInputNotificationAction actionWithIdentifier:@"replyAction" title:@"文字回复" options:UNNotificationActionOptionNone];
//点击进入应用
UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"enterAction" title:@"进入应用" options:UNNotificationActionOptionForeground];
//点击取消,没有任何操作
UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"cancelAction" title:@"取消" options:UNNotificationActionOptionDestructive];
//通过UNNotificationCategory对象将这几个action行为添加到通知里去
UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"Categroy" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
//将categroy赋值到通知内容上
content.categoryIdentifier = @"Categroy";
//设置通知代理,用于检测点击方法
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
/* add request and notificaiton code ... */

效果如下:



notificaiotn04.png



notification05.png

获取通知交互内容:

//识别通知交互处理的代理方法
- (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"]){
//识别用户点击的是哪个 action
UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse*)response;
//获取输入内容
NSString *userText = textResponse.userText;
//发送 userText 给需要接收的方法
NSLog(@"要发送的内容是:%@",userText);
//[ClassName handleUserText: userText];
}else if([response.actionIdentifier isEqualToString:@"enterAction"]){
NSLog(@"点击了进入应用按钮");
}else{
NSLog(@"点击了取消");
}
}
completionHandler();
}

由此我们可以知道action,categroy,request这些东西都是通过各自的identifier获取的。这样可以很方便的定位到某一个通知或者action上,为交互的处理提供了很大的便利。

自定义通知样式

在Xcode中File->New->Targe会出现下面的视图



notification06.png

Notification Content对应的是通知,Notification Service Extension对应的是推送。我们这里要实现通知的自定义,选择左边那个。创建成功之后会在工程里多一个文件件



notification07.png

NotificationViewController文件是自动生成的,里面有一个
- (void)didReceiveNotification:(UNNotification *)notification


可以在这里定义一些通知的显示。

MainInterface.storyboard文件是控制通知的storyboard文件,可以编辑需要的通知样式。我们设计一下文字的颜色和显示位置



notificaiton08.png

接下来你可能会问,怎么把这个自定义的通知样式应用到当前通知里呢?先别急,我们看下一个文件Info.flist里面的内容



notification09.png

第一项
UNNotificationExtensionCategory
就是UNNotificationCategory的标示,我们把他换成我们通知里使用的标示
"Category"
,系统就会自动匹配通知显示的样式。

第二项
UNNotificationExtensionIntialContentSizeRation
初始内容 Size 的比例。也可以在 viewDidLoad 中使用 self.preferredContentSize 直接设置 Size。

第三项
UNNotificationExtensionDefaultContentHidden
是否隐藏默认内容,如果设为YES,默认内容会被隐藏。

显示的效果:



notification10.png

原文链接:http://www.jianshu.com/p/5713fa2bfece
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息