UNUserNotificationCenter是iOS10 推出的新的通知中心 ,最近的项目涉及的比较深,我就总结了一下:
下面我们开始一步一步的来添加本地推送,
1、首先在开始注册通知:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//监听回调事件
center.delegate = self;
//iOS 10 使用以下方法注册,才能得到授权
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound +UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
//获取当前的通知设置,UNNotificationSettings 是只读对象,不能直接修改,只能通过以下方法获取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}];
我们要首先注册通知中心的代理事件,注册通知,提醒用户获取通知权限:
- (void)requestAuthorizationWithOptions:(UNAuthorizationOptions)options completionHandler:(void (^)(BOOL granted, NSError *__nullable error))completionHandler;
(1)其中 UNAuthorizationOptions 是授权通知提醒的方式
UNAuthorizationOptionBa dge;
UNAuthorizationOptionSound;
UNAuthorizationOptionAlert;
UNAuthorizationOptionCarPlay;
这个根据需要填写。
(2)block中的 BOOL granted 与 NSError *__nullable error :
error 注册失败时错误抛出;
granted 为YES时得到用户授权,为NO�时授权失败 (用户不允许);
2、创建一个新的通知内容 UNMutableNotificationContent ,我们先了解一下这个类:
// 为通知中附加音频,图片,视频
@property (NS_NONATOMIC_IOSONLY, copy) NSArray <UNNotificationAttachment *> *attachments
//控制应用的badge的数量
@property (NS_NONATOMIC_IOSONLY, copy, nullable) NSNumber *badge;
//通知的消息体
@property (NS_NONATOMIC_IOSONLY, copy) NSString *body ;
//在通知代理 中用来识别具体是哪一个通知
@property (NS_NONATOMIC_IOSONLY, copy) NSString
*categoryIdentifier ;
//通知中有图片时,设置本属性,可以获取下拉图片的大图展示
@property (NS_NONATOMIC_IOSONLY, copy) NSString *launchImageName;
//通知的提示音,可以默认系统的defaultSound ; 也可以加载本地的音频文件(传入音频文件名),这里iOS10中有个BUG, 加载本地音频文件时有时会放不出来
@property (NS_NONATOMIC_IOSONLY, copy, nullable) UNNotificationSound *sound ;
// 通知的副标题
@property (NS_NONATOMIC_IOSONLY, copy) NSString *subtitle ;
//线程标识
@property (NS_NONATOMIC_IOSONLY, copy) NSString *threadIdentifier;
//通知的标题
@property (NS_NONATOMIC_IOSONLY, copy) NSString *title;
//当一个通知发出时携带的信息;(远程时用到)
@property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *userInfo;
下面看一下笔者写的一个方法:
/**
IOS 10的通知 推送消息 支持的音频 <= 5M(现有的系统偶尔会出现播放不出来的BUG) 图片 <= 10M 视频 <= 50M ,这些后面都要带上格式;
@param body 消息内容
@param promptTone 提示音
@param soundName 音频
@param imageName 图片
@param movieName 视频
@param identifier 消息标识
*/
-(void)pushNotification_IOS_10_Body:(NSString *)body
promptTone:(NSString *)promptTone
soundName:(NSString *)soundName
imageName:(NSString *)imageName
movieName:(NSString *)movieName
Identifier:(NSString *)identifier {
//获取通知中心用来激活新建的通知
UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc]init];
content.body = body;
//通知的提示音
if ([promptTone containsString:@"."]) {
UNNotificationSound *sound = [UNNotificationSound soundNamed:promptTone];
content.sound = sound;
}
__block UNNotificationAttachment *imageAtt;
__block UNNotificationAttachment *movieAtt;
__block UNNotificationAttachment *soundAtt;
if ([imageName containsString:@"."]) {
[self addNotificationAttachmentContent:content attachmentName:imageName options:nil withCompletion:^(NSError *error, UNNotificationAttachment *notificationAtt) {
imageAtt = [notificationAtt copy];
}];
}
if ([soundName containsString:@"."]) {
[self addNotificationAttachmentContent:content attachmentName:soundName options:nil withCompletion:^(NSError *error, UNNotificationAttachment *notificationAtt) {
soundAtt = [notificationAtt copy];
}];
}
if ([movieName containsString:@"."]) {
// 在这里截取视频的第10s为视频的缩略图 :UNNotificationAttachmentOptionsThumbnailTimeKey
[self addNotificationAttachmentContent:content attachmentName:movieName options:@{@"UNNotificationAttachmentOptionsThumbnailTimeKey":@10} withCompletion:^(NSError *error, UNNotificationAttachment *notificationAtt) {
movieAtt = [notificationAtt copy];
}];
}
NSMutableArray * array = [NSMutableArray array];
// [array addObject:soundAtt];
// [array addObject:imageAtt];
[array addObject:movieAtt];
content.attachments = array;
//添加通知下拉动作按钮
NSMutableArray * actionMutableArray = [NSMutableArray array];
UNNotificationAction * actionA = [UNNotificationAction actionWithIdentifier:@"identifierNeedUnlock" title:@"进入应用" options:UNNotificationActionOptionAuthenticationRequired];
UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"identifierRed" title:@"忽略" options:UNNotificationActionOptionDestructive];
[actionMutableArray addObjectsFromArray:@[actionA,actionB]];
if (actionMutableArray.count > 1) {
UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"categoryNoOperationAction" actions:actionMutableArray intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
[center setNotificationCategories:[NSSet setWithObjects:category, nil]];
content.categoryIdentifier = @"categoryNoOperationAction";
}
//UNTimeIntervalNotificationTrigger 延时推送
//UNCalendarNotificationTrigger 定时推送
//UNLocationNotificationTrigger 位置变化推送
UNTimeIntervalNotificationTrigger * tirgger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];
//建立通知请求
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:tirgger];
//将建立的通知请求添加到通知中心
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"%@本地推送 :( 报错 %@",identifier,error);
}];
}
/**
增加通知附件
@param content 通知内容
@param attachmentName 附件名称
@param options 相关选项
@param completion 结果回调
*/
-(void)addNotificationAttachmentContent:(UNMutableNotificationContent *)content attachmentName:(NSString *)attachmentName options:(NSDictionary *)options withCompletion:(void(^)(NSError * error , UNNotificationAttachment * notificationAtt))completion{
NSArray * arr = [attachmentName componentsSeparatedByString:@"."];
NSError * error;
NSString * path = [[NSBundle mainBundle]pathForResource:arr[0] ofType:arr[1]];
UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:[NSString stringWithFormat:@"notificationAtt_%@",arr[1]] URL:[NSURL fileURLWithPath:path] options:options error:&error];
if (error) {
NSLog(@"attachment error %@", error);
}
completion(error,attachment);
//获取通知下拉放大图片
content.launchImageName = attachmentName;
}
屏幕快照 2017-01-09 17.40.57.png
屏幕快照 2017-01-09 17.40.26.png
屏幕快照 2017-01-10 15.55.26.png
其中通知中的 图片、语音、视频,经过自己测试只能同时添加,只能显示其中一种,这个我还没有搞清楚 。
在创建附件的方法:attachmentWithIdentifier:URL:options:error:中,
(1)URL必须是一个有效地文件路径,
(2)option 是共有4个选项,
UNNotificationAttachmentOptionsTypeHintKey : 如果添加附件的文件名字中没有类型,就要靠该键值来确定文件类型;
UNNotificationAttachmentOptionsThumbnailHiddenKey : 是一个BOOL值,为YES时候,缩略图将隐藏,默认为YES;
UNNotificationAttachmentOptionsThumbnailClippingRectKey : 是一个矩形CGRect 剪贴图片的缩略图的键值;
UNNotificationAttachmentOptionsThumbnailTimeKey : 如果附件是一个视频的话,可以用这值来指定视频中的某一秒为视频的缩略图;
关于通知的我们可以设置它延时提醒,定时提醒(闹钟),位置发生变化提醒:
UNTimeIntervalNotificationTrigger 延时推送
UNCalendarNotificationTrigger 定时推送
UNLocationNotificationTrigger 位置变化推送
(1)关于UNTimeIntervalNotificationTrigger 通知延时设置,注意 :如果 repeats 设置为YES 则延时时间必须大于 60s 否则会 crash;
(2)UNCalendarNotificationTrigger 使用定时推送 ,通过类NSDateComponents 来设置具体的提醒日期;
(3)UNLocationNotificationTrigger 使用位置变化触发推送,通过类CLRegion 来设置经纬度;
3、通知中的代理事件
//当应用在前台时,收到通知会触发这个代理方法;你可以在这个方法中对应用处于前台时接到通知做一些自己的处理
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionAlert);
}
//当点击进入应用时触发;
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionAlert);
}
这里complitionHandler中参数是UNNotificationPresentationOptions 通知提醒的方式 :
UNNotificationPresentationOptionBadge ,
UNNotificationPresentationOptionSound ,
UNNotificationPresentationOptionAlert ,
欢迎大家 看过后,对文章做出评价,与指导!关于推送的更多内容我会不断的完善更新。
网友评论