最近在研究ios10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点:
1.推送内容更加丰富,由之前的alert 到现在的title, subtitle, body
2.推送统一由trigger触发
3.可以为推送增加附件,如图片、音频、视频,这就使推送内容更加丰富多彩
4.可以方便的更新推送内容
import 新框架
添加新的框架 usernotifications.framework
#import <usernotifications/usernotifications.h>
注册推送
在设置通知的时候,需要先进行注册,获取授权
ios10 所有通知都是通过unusernotificationcenter来管理,包括远程通知和本地通知
1
2
3
4
5
6
7
8
9
10
11
|
//ios8以下
[application registerforremotenotificationtypes:uiremotenotificationtypebadge | uiremotenotificationtypealert | uiremotenotificationtypesound];
//ios8 - ios10
[application registerusernotificationsettings:[uiusernotificationsettings settingsfortypes:uiusernotificationtypealert | uiusernotificationtypesound | uiusernotificationtypebadge categories:nil]];
//ios10
unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter];
[center requestauthorizationwithoptions:(unauthorizationoptionalert | unauthorizationoptionbadge | unauthorizationoptionsound) completionhandler:^( bool granted, nserror * _nullable error) {
}
|
获取用户设置
ios10 提供了获取用户授权相关设置信息的接口getnotificationsettingswithcompletionhandler: , 回调带有一个unnotificationsettings对象,它具有以下属性,可以准确获取各种授权信息
authorizationstatus
soundsetting
badgesetting
alertsetting
notificationcentersetting
lockscreensetting
carplaysetting
alertstyle
像下面的方法,点击allow
1
2
3
4
5
6
7
8
9
10
11
12
13
|
unusernotificationcenter *center = [unusernotificationcenter currentnotificationcenter];
[center requestauthorizationwithoptions:(unauthorizationoptionalert | unauthorizationoptionbadge | unauthorizationoptionsound) completionhandler:^( bool granted, nserror * _nullable error) {
if (granted) {
//点击允许
nslog(@ "注册通知成功" );
[center getnotificationsettingswithcompletionhandler:^(unnotificationsettings * _nonnull settings) {
nslog(@ "%@" , settings);
}];
} else {
//点击不允许
nslog(@ "注册通知失败" );
}
}];
|
打印信息: *<unnotificationsettings: 0x174090a90; authorizationstatus: authorized, notificationcentersetting: enabled, soundsetting: enabled, badgesetting: enabled, lockscreensetting: enabled, alertsetting: notsupported, carplaysetting: enabled, alertstyle: banner>*
注册apns, 获取token
ios10, 注册apns和获取token的方法还和之前一样
在application: didfinishlaunchingwithoptions:调用 registerforremotenotifications方法
[[uiapplication sharedapplication] registerforremotenotifications];
在代理方法application: didregisterforremotenotificationswithdevicetoken:中获取token
1
2
3
4
5
6
7
8
|
- ( void )application:(uiapplication *)application didregisterforremotenotificationswithdevicetoken:(nsdata *)devicetoken ns_available_ios(3_0){
nslog(@ "devicetoken:%@" ,devicetoken);
}
- ( void )application:(uiapplication *)application didfailtoregisterforremotenotificationswitherror:(nserror *)error ns_available_ios(3_0){
nslog(@ "didfailtoregisterforremotenotificationswitherror:%@" ,error);
}
|
设置处理通知的action 和 category
在ios8以前是没有category这个属性的;
在ios8注册推送,获取授权的时候,可以一并设置category, 注册的方法直接带有这个参数;
在ios10, 需要调用一个方法setnotificationcategories:来为管理推送的unusernotificationcenter实例设置category, category又可以对应设置action;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//设置category
//unnotificationactionoptionauthenticationrequired 需要解锁
//unnotificationactionoptiondestructive 显示为红色
//unnotificationactionoptionforeground 点击打开app
unnotificationaction *action1 = [unnotificationaction actionwithidentifier:@ "action1" title:@ "策略1行为1" options:unnotificationactionoptionforeground];
untextinputnotificationaction *action2 = [untextinputnotificationaction actionwithidentifier:@ "action2" title:@ "策略1行为2" options:unnotificationactionoptiondestructive textinputbuttontitle:@ "comment" textinputplaceholder:@ "reply" ];
//unnotificationcategoryoptionnone
//unnotificationcategoryoptioncustomdismissaction 清除通知被触发会走通知的代理方法
//unnotificationcategoryoptionallowincarplay 适用于行车模式
unnotificationcategory *category1 = [unnotificationcategory categorywithidentifier:@ "category1" actions:@[action2,action1] minimalactions:@[action2,action1] intentidentifiers:@[] options:unnotificationcategoryoptioncustomdismissaction];
unnotificationaction *action3 = [unnotificationaction actionwithidentifier:@ "action3" title:@ "策略2行为1" options:unnotificationactionoptionforeground];
unnotificationaction *action4 = [unnotificationaction actionwithidentifier:@ "action4" title:@ "策略2行为2" options:unnotificationactionoptionforeground];
unnotificationcategory *category2 = [unnotificationcategory categorywithidentifier:@ "category2" actions:@[action3,action4] minimalactions:@[action3,action4] intentidentifiers:@[] options:unnotificationcategoryoptioncustomdismissaction];
[[unusernotificationcenter currentnotificationcenter] setnotificationcategories:[nsset setwithobjects:category1,category2, nil]];
|
设置通知内容
因为ios10远程通知与本地通知统一起来了,通知内容属性是一致的,不过远程推送就需要在payload进行具体设置了,下面以本地通知为例,介绍关于unnotificationcontent的内容
官网上明确说明了,我们是不能直接创建unnotificationcontent的实例的, 如果我们需要自己去配置内容的各个属性,我们需要用到unmutablenotificationcontent
看一下它的一些属性:
attachments //附件
badge //徽标
body //推送内容body
categoryidentifier //category标识
launchimagename //点击通知进入应用的启动图
sound //声音
subtitle //推送内容子标题
title //推送内容标题
userinfo //远程通知内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
unmutablenotificationcontent *content = [[unmutablenotificationcontent alloc] init];
content.title = @ "test" ;
content.subtitle = @ "1234567890" ;
content.body = @ "copyright © 2016年 jpush. all rights reserved." ;
content.badge = @1;
nserror *error = nil;
nsstring *path = [[nsbundle mainbundle] pathforresource:@ "718835727" oftype:@ "png" ];
unnotificationattachment *att = [unnotificationattachment attachmentwithidentifier:@ "att1" url:[nsurl fileurlwithpath:path] options:nil error:&error];
if (error) {
nslog(@ "attachment error %@" , error);
}
content.attachments = @[att];
content.categoryidentifier = @"category1”; //这里设置category1, 是与之前设置的category对应
content.launchimagename = @ "1-eb_0ovtcxjxhz7-ioobsaq" ;
unnotificationsound *sound = [unnotificationsound defaultsound];
content.sound = sound;
|
通知触发器
unnotificationtrigger
ios 10触发器有4种
•unpushnotificationtrigger 触发apns服务,系统自动设置(这是区分本地通知和远程通知的标识)
•untimeintervalnotificationtrigger 一段时间后触发
•uncalendarnotificationtrigger 指定日期触发
•unlocationnotificationtrigger 根据位置触发,支持进入某地或者离开某地或者都有
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//十秒后
untimeintervalnotificationtrigger *trigger1 = [untimeintervalnotificationtrigger triggerwithtimeinterval:10 repeats:no];
//每周日早上8:00
nsdatecomponents *component = [[nsdatecomponents alloc] init];
component.weekday = 1;
component.hour = 8;
uncalendarnotificationtrigger *trigger2 = [uncalendarnotificationtrigger triggerwithdatematchingcomponents:component repeats:yes];
//圆形区域,进入时候进行通知
cllocationcoordinate2d cen = cllocationcoordinate2dmake(80.335400, -90.009201);
clcircularregion *region = [[clcircularregion alloc] initwithcenter:cen
radius:500.0 identifier:@“center"];
region.notifyonentry = yes; //进入的时候
region.notifyonexit = no; //出去的时候
unlocationnotificationtrigger *trigger3 = [unlocationnotificationtrigger
triggerwithregion:region repeats:no];
|
添加通知 / 更新通知
1.创建一个unnotificationrequest类的实例,一定要为它设置identifier, 在后面的查找,更新, 删除通知,这个标识是可以用来区分这个通知与其他通知
2.把request加到unusernotificationcenter, 并设置触发器,等待触发
3.
如果另一个request具有和之前request相同的标识,不同的内容, 可以达到更新通知的目的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
nsstring *requestidentifer = @ "testrequest" ;
unnotificationrequest *request = [unnotificationrequest requestwithidentifier:requestidentifer content:content trigger:trigger1];
//把通知加到unusernotificationcenter, 到指定触发点会被触发
[center addnotificationrequest:request withcompletionhandler:^(nserror * _nullable error) {
}];
//在另外需要更新通知的地方
unmutablenotificationcontent *newcontent = [[unmutablenotificationcontent alloc] init];
newcontent.title = @ "update" ;
newcontent.subtitle = @ "xxxxxxxxx" ;
newcontent.body = @ "copyright © 2016年 jpush. all rights reserved." ;
untimeintervalnotificationtrigger *trigger1 = [untimeintervalnotificationtrigger triggerwithtimeinterval:3 repeats:no];
unnotificationrequest *request = [unnotificationrequest requestwithidentifier:@ "testrequest" content:newcontent trigger:trigger1];
[[unusernotificationcenter currentnotificationcenter] addnotificationrequest:request withcompletionhandler:^(nserror * _nullable error) {
}];
|
获取和删除通知
这里通知是有两种状态
•pending 等待触发的通知
•delivered 已经触发展示在通知中心的通知
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//获取未触发的通知
[[unusernotificationcenter currentnotificationcenter] getpendingnotificationrequestswithcompletionhandler:^(nsarray<unnotificationrequest *> * _nonnull requests) {
nslog(@ "pending: %@" , requests);
}];
//获取通知中心列表的通知
[[unusernotificationcenter currentnotificationcenter] getdeliverednotificationswithcompletionhandler:^(nsarray<unnotification *> * _nonnull notifications) {
nslog(@ "delivered: %@" , notifications);
}];
//清除某一个未触发的通知
[[unusernotificationcenter currentnotificationcenter] removependingnotificationrequestswithidentifiers:@[@ "testrequest1" ]];
//清除某一个通知中心的通知
[[unusernotificationcenter currentnotificationcenter] removedeliverednotificationswithidentifiers:@[@ "testrequest2" ]];
//对应的删除所有通知
[[unusernotificationcenter currentnotificationcenter] removeallpendingnotificationrequests];
[[unusernotificationcenter currentnotificationcenter] removealldeliverednotifications];
|
delegate
<unusernotificationcenterdelegate>
ios10收到通知不再是在application: didreceiveremotenotification:方法去处理, ios10推出新的代理方法,接收和处理各类通知(本地或者远程)
1
2
3
4
5
6
7
8
9
|
- ( void )usernotificationcenter:(unusernotificationcenter *)center willpresentnotification:(unnotification *)notification withcompletionhandler:( void (^)(unnotificationpresentationoptions))completionhandler {
//应用在前台收到通知
nslog(@ "========%@" , notification);
}
- ( void )usernotificationcenter:(unusernotificationcenter *)center didreceivenotificationresponse:(unnotificationresponse *)response withcompletionhandler:( void (^)())completionhandler {
//点击通知进入应用
nslog(@ "response:%@" , response);
}
|
最后
下一篇文章继续介绍关于富媒体推送的 unnotificationserviceextension 和 notification content extension, 未完待续。。。