iOS10 关于推送.docx
- 文档编号:15974391
- 上传时间:2023-07-09
- 格式:DOCX
- 页数:10
- 大小:17.80KB
iOS10 关于推送.docx
《iOS10 关于推送.docx》由会员分享,可在线阅读,更多相关《iOS10 关于推送.docx(10页珍藏版)》请在冰点文库上搜索。
iOS10关于推送
iOS10关于推送
最近在研究iOS10关于推送的新特性,相比之前确实做了很大的改变,总结起来主要是以下几点:
推送内容更加丰富,由之前的alert到现在的title,subtitle,body
推送统一由trigger触发
可以为推送增加附件,如图片、音频、视频,这就使推送内容更加丰富多彩
可以方便的更新推送内容
import新框架
添加新的框架UserNotifications.framework#import
获取推送权限
在设置通知的时候,需要先进行注册,获取授权
iOS10所有通知都是通过UNUserNotificationCenter来管理,包括远程通知和本地通知
//iOS8以下
[applicationregisterForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound];
//iOS8-iOS10
[applicationregisterUserNotificationSettings:
[UIUserNotificationSettingssettingsForTypes:
UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadgecategories:
nil]];
//iOS10
UNUserNotificationCenter*center=[UNUserNotificationCentercurrentNotificationCenter];
[centerrequestAuthorizationWithOptions:
(UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound)completionHandler:
^(BOOLgranted,NSError*_Nullableerror){
}
获取用户设置
iOS10提供了获取用户授权相关设置信息的接口getNotificationSettingsWithCompletionHandler:
回调带有一个UNNotificationSettings对象,它具有以下属性,可以准确获取各种授权信息
authorizationStatus
soundSetting
badgeSetting
alertSetting
notificationCenterSetting
lockScreenSetting
carPlaySetting
alertStyle
像下面的方法,点击allowUNUserNotificationCenter*center=[UNUserNotificationCentercurrentNotificationCenter];
[centerrequestAuthorizationWithOptions:
(UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound)completionHandler:
^(BOOLgranted,NSError*_Nullableerror){
if(granted){
//点击允许
NSLog(@"注册通知成功");
[centergetNotificationSettingsWithCompletionHandler:
^(UNNotificationSettings*_Nonnullsettings){
NSLog(@"%@",settings);
}];
}else{
//点击不允许
NSLog(@"注册通知失败");
}
}];
打印信息:
**
注册APNS,获取token
iOS10,注册APNS和获取token的方法还和之前一样
在application:
didFinishLaunchingWithOptions:
调用registerForRemoteNotifications方法
[[UIApplicationsharedApplication]registerForRemoteNotifications];
在代理方法application:
didRegisterForRemoteNotificationsWithDeviceToken:
中获取token
-(void)application:
(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:
(NSData*)deviceTokenNS_AVAILABLE_IOS(3_0){
NSLog(@"deviceToken:
%@",deviceToken);
}
-(void)application:
(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:
(NSError*)errorNS_AVAILABLE_IOS(3_0){
NSLog(@"didFailToRegisterForRemoteNotificationsWithError:
%@",error);
}
设置处理通知的action和category
在iOS8以前是没有category这个属性的;
在iOS8注册推送,获取授权的时候,可以一并设置category,注册的方法直接带有这个参数;
在iOS10,需要调用一个方法setNotificationCategories:
来为管理推送的UNUserNotificationCenter实例设置category,category又可以对应设置action;
//设置category
//UNNotificationActionOptionAuthenticationRequired需要解锁
//UNNotificationActionOptionDestructive显示为红色
//UNNotificationActionOptionForeground点击打开app
UNNotificationAction*action1=[UNNotificationActionactionWithIdentifier:
@"action1"title:
@"策略1行为1"options:
UNNotificationActionOptionForeground];
UNTextInputNotificationAction*action2=[UNTextInputNotificationActionactionWithIdentifier:
@"action2"title:
@"策略1行为2"options:
UNNotificationActionOptionDestructivetextInputButtonTitle:
@"comment"textInputPlaceholder:
@"reply"];
//UNNotificationCategoryOptionNone
//UNNotificationCategoryOptionCustomDismissAction清除通知被触发会走通知的代理方法
//UNNotificationCategoryOptionAllowInCarPlay适用于行车模式
UNNotificationCategory*category1=[UNNotificationCategorycategoryWithIdentifier:
@"category1"actions:
@[action2,action1]minimalActions:
@[action2,action1]intentIdentifiers:
@[]options:
UNNotificationCategoryOptionCustomDismissAction];
UNNotificationAction*action3=[UNNotificationActionactionWithIdentifier:
@"action3"title:
@"策略2行为1"options:
UNNotificationActionOptionForeground];
UNNotificationAction*action4=[UNNotificationActionactionWithIdentifier:
@"action4"title:
@"策略2行为2"options:
UNNotificationActionOptionForeground];
UNNotificationCategory*category2=[UNNotificationCategorycategoryWithIdentifier:
@"category2"actions:
@[action3,action4]minimalActions:
@[action3,action4]intentIdentifiers:
@[]options:
UNNotificationCategoryOptionCustomDismissAction];
[[UNUserNotificationCentercurrentNotificationCenter]setNotificationCategories:
[NSSetsetWithObjects:
category1,category2,nil]];
设置通知内容
因为iOS10远程通知与本地通知统一起来了,通知内容属性是一致的,不过远程推送就需要在payload进行具体设置了,下面以本地通知为例,介绍关于UNNotificationContent的内容
官网上明确说明了,我们是不能直接创建UNNotificationContent的实例的,如果我们需要自己去配置内容的各个属性,我们需要用到UNMutableNotificationContent
看一下它的一些属性:
attachments//附件
badge//徽标
body//推送内容body
categoryIdentifier//category标识
launchImageName//点击通知进入应用的启动图
sound//声音
subtitle//推送内容子标题
title//推送内容标题
userInfo//远程通知内容
UNMutableNotificationContent*content=[[UNMutableNotificationContentalloc]init];
content.title=@"Test";
content.subtitle=@"1234567890";
content.body=@"Copyright?
2016年jpush.Allrightsreserved.";
content.badge=@1;
NSError*error=nil;
NSString*path=[[NSBundlemainBundle]pathForResource:
@"718835727"ofType:
@"png"];
UNNotificationAttachment*att=[UNNotificationAttachmentattachmentWithIdentifier:
@"att1"URL:
[NSURLfileURLWithPath:
path]options:
nilerror:
&error];
if(error){
NSLog(@"attachmenterror%@",error);
}
content.attachments=@[att];
content.categoryIdentifier=@"category1”;//这里设置category1,是与之前设置的category对应
content.launchImageName=@"1-Eb_0OvtcxJXHZ7-IOoBsaQ";
UNNotificationSound*sound=[UNNotificationSounddefaultSound];
content.sound=sound;
通知触发器
UNNotificationTrigger
iOS10触发器有4种
UNPushNotificationTrigger触发APNS服务,系统自动设置(这是区分本地通知和远程通知的标识)
UNTimeIntervalNotificationTrigger一段时间后触发
UNCalendarNotificationTrigger指定日期触发
UNLocationNotificationTrigger根据位置触发,支持进入某地或者离开某地或者都有
//十秒后
UNTimeIntervalNotificationTrigger*trigger1=[UNTimeIntervalNotificationTriggertriggerWithTimeInterval:
10repeats:
NO];
//每周日早上8:
00
NSDateComponents*component=[[NSDateComponentsalloc]init];
component.weekday=1;
component.hour=8;
UNCalendarNotificationTrigger*trigger2=[UNCalendarNotificationTriggertriggerWithDateMatchingComponents:
componentrepeats:
YES];
//圆形区域,进入时候进行通知
CLLocationCoordinate2Dcen=CLLocationCoordinate2DMake(80.335400,-90.009201);
CLCircularRegion*region=[[CLCircularRegionalloc]initWithCenter:
cen
radius:
500.0identifier:
@“center"];
region.notifyOnEntry=YES;//进入的时候
region.notifyOnExit=NO;//出去的时候
UNLocationNotificationTrigger*trigger3=[UNLocationNotificationTrigger
triggerWithRegion:
regionrepeats:
NO];
添加通知/更新通知
创建一个UNNotificationRequest类的实例,一定要为它设置identifier,在后面的查找,更新,删除通知,这个标识是可以用来区分这个通知与其他通知
把request加到UNUserNotificationCenter,并设置触发器,等待触发
如果另一个request具有和之前request相同的标识,不同的内容,可以达到更新通知的目的
温馨提示:
这个identifier千万不要设置成@”“(空字符串),否则手机就黑屏了,需要重新刷机。
亲测属实,不要手贱,具体查看同事发表的文章链接
NSString*requestIdentifer=@"TestRequest";
UNNotificationRequest*request=[UNNotificationRequestrequestWithIdentifier:
requestIdentifercontent:
contenttrigger:
trigger1];
//把通知加到UNUserNotificationCenter,到指定触发点会被触发
[centeraddNotificationRequest:
requestwithCompletionHandler:
^(NSError*_Nullableerror){
}];
//在另外需要更新通知的地方
UNMutableNotificationContent*newContent=[[UNMutableNotificationContentalloc]init];
newContent.title=@"Update";
newContent.subtitle=@"XXXXXXXXX";
newContent.body=@"Copyright?
2016年jpush.Allrightsreserved.";
UNTimeIntervalNotificationTrigger*trigger1=[UNTimeIntervalNotificationTriggertriggerWithTimeInterval:
3repeats:
NO];
UNNotificationRequest*request=[UNNotificationRequestrequestWithIdentifier:
@"TestRequest"content:
newContenttrigger:
trigger1];
[[UNUserNotificationCentercurrentNotificationCenter]addNotificationRequest:
requestwithCompletionHandler:
^(NSError*_Nullableerror){
}];获取和删除通知
这里通知是有两种状态
*Pending等待触发的通知
*Delivered已经触发展示在通知中心的通知
//获取未触发的通知
[[UNUserNotificationCentercurrentNotificationCenter]getPendingNotificationRequestsWithCompletionHandler:
^(NSArray*_Nonnullrequests){
NSLog(@"pending:
%@",requests);
}];
//获取通知中心列表的通知
[[UNUserNotificationCentercurrentNotificationCenter]getDeliveredNotificationsWithCompletionHandler:
^(NSArray*_Nonnullnotifications){
NSLog(@"Delivered:
%@",notifications);
}];
//清除某一个未触发的通知
[[UNUserNotificationCentercurrentNotificationCenter]removePendingNotificationRequestsWithIdentifiers:
@[@"TestRequest1"]];
//清除某一个通知中心的通知
[[UNUserNotificationCentercurrentNotificationCenter]removeDeliveredNotificationsWithIdentifiers:
@[@"TestRequest2"]];
//对应的删除所有通知
[[UNUserNotificationCentercurrentNotificationCenter]removeAllPendingNotificationRequests];
[[UNUserNotificationCentercurrentNotificationCenter]removeAllDeliveredNotifications];
delegate
iOS10收到通知不再是在application:
didReceiveRemoteNotification:
方法去处理,iOS10推出新的代理方法,接收和处理各类通知(本地或者远程)
-(void)userNotificationCenter:
(UNUserNotificationCenter*)centerwillPresentNotification:
(UNNotification*)notificationwithCompletionHandler:
(void(^)(UNNotificationPresentationOptions))completionHandler{
//应用在前台收到通知
NSLog(@"========%@",notification);
//如果需要在应用在前台也展示通知
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:
(UNUserNotificationCenter*)centerdidReceiveNotificationResponse:
(UNNotificationResponse*)responsewithCompletionHandler:
(void(^)())completionHandler{
//点击通知进入应用
NSLog(@"response:
%@",response);
}
最后
下一篇文章继续介绍关于富媒体推送的UNNotificationServiceExtension和Notificationcontentextension,未完待续。
。
。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- iOS10 关于推送 关于 推送