UNUserNotificationCenter是iOS10 推出的新的通知中心 ,最近的項目涉及的比較深,我就總結(jié)了一下:
下面我們開始一步一步的來添加本地推送面褐,
1、首先在開始注冊通知:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
//監(jiān)聽回調(diào)事件
center.delegate = self;
//iOS 10 使用以下方法注冊取胎,才能得到授權(quán)
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound +UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
//獲取當(dāng)前的通知設(shè)置展哭,UNNotificationSettings 是只讀對象,不能直接修改闻蛀,只能通過以下方法獲取
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
}];
我們要首先注冊通知中心的代理事件匪傍,注冊通知,提醒用戶獲取通知權(quán)限:
- (void)requestAuthorizationWithOptions:(UNAuthorizationOptions)options completionHandler:(void (^)(BOOL granted, NSError *__nullable error))completionHandler;
(1)其中 UNAuthorizationOptions 是授權(quán)通知提醒的方式
UNAuthorizationOptionBa dge觉痛;
UNAuthorizationOptionSound役衡;
UNAuthorizationOptionAlert;
UNAuthorizationOptionCarPlay薪棒;
這個根據(jù)需要填寫手蝎。
(2)block中的 BOOL granted 與 NSError *__nullable error :
error 注冊失敗時錯誤拋出;
granted 為YES時得到用戶授權(quán)俐芯,為NO?時授權(quán)失敗 (用戶不允許)棵介;
2、創(chuàng)建一個新的通知內(nèi)容 UNMutableNotificationContent ,我們先了解一下這個類:
// 為通知中附加音頻,圖片锡溯,視頻
@property (NS_NONATOMIC_IOSONLY, copy) NSArray <UNNotificationAttachment *> *attachments
//控制應(yīng)用的badge的數(shù)量
@property (NS_NONATOMIC_IOSONLY, copy, nullable) NSNumber *badge;
//通知的消息體
@property (NS_NONATOMIC_IOSONLY, copy) NSString *body ;
//在通知代理 中用來識別具體是哪一個通知
@property (NS_NONATOMIC_IOSONLY, copy) NSString
*categoryIdentifier ;
//通知中有圖片時淳地,設(shè)置本屬性累颂,可以獲取下拉圖片的大圖展示
@property (NS_NONATOMIC_IOSONLY, copy) NSString *launchImageName;
//通知的提示音县貌,可以默認系統(tǒng)的defaultSound 琳状; 也可以加載本地的音頻文件(傳入音頻文件名)绵跷,這里iOS10中有個BUG锐极, 加載本地音頻文件時有時會放不出來
@property (NS_NONATOMIC_IOSONLY, copy, nullable) UNNotificationSound *sound ;
// 通知的副標(biāo)題
@property (NS_NONATOMIC_IOSONLY, copy) NSString *subtitle ;
//線程標(biāo)識
@property (NS_NONATOMIC_IOSONLY, copy) NSString *threadIdentifier;
//通知的標(biāo)題
@property (NS_NONATOMIC_IOSONLY, copy) NSString *title;
//當(dāng)一個通知發(fā)出時攜帶的信息笙僚;(遠程時用到)
@property (NS_NONATOMIC_IOSONLY, copy) NSDictionary *userInfo;
下面看一下筆者寫的一個方法:
/**
IOS 10的通知 推送消息 支持的音頻 <= 5M(現(xiàn)有的系統(tǒng)偶爾會出現(xiàn)播放不出來的BUG) 圖片 <= 10M 視頻 <= 50M ,這些后面都要帶上格式灵再;
@param body 消息內(nèi)容
@param promptTone 提示音
@param soundName 音頻
@param imageName 圖片
@param movieName 視頻
@param identifier 消息標(biāo)識
*/
-(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:@"進入應(yīng)用" 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 通知內(nèi)容
@param attachmentName 附件名稱
@param options 相關(guān)選項
@param completion 結(jié)果回調(diào)
*/
-(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;
}
其中通知中的 圖片肋层、語音、視頻翎迁,經(jīng)過自己測試只能同時添加栋猖,只能顯示其中一種,這個我還沒有搞清楚 汪榔。
在創(chuàng)建附件的方法:attachmentWithIdentifier:URL:options:error:中蒲拉,
(1)URL必須是一個有效地文件路徑,
(2)option 是共有4個選項痴腌,
UNNotificationAttachmentOptionsTypeHintKey : 如果添加附件的文件名字中沒有類型雌团,就要靠該鍵值來確定文件類型;
UNNotificationAttachmentOptionsThumbnailHiddenKey : 是一個BOOL值士聪,為YES時候锦援,縮略圖將隱藏,默認為YES剥悟;
UNNotificationAttachmentOptionsThumbnailClippingRectKey : 是一個矩形CGRect 剪貼圖片的縮略圖的鍵值灵寺;
UNNotificationAttachmentOptionsThumbnailTimeKey : 如果附件是一個視頻的話,可以用這值來指定視頻中的某一秒為視頻的縮略圖区岗;
關(guān)于通知的我們可以設(shè)置它延時提醒略板,定時提醒(鬧鐘),位置發(fā)生變化提醒:
UNTimeIntervalNotificationTrigger 延時推送
UNCalendarNotificationTrigger 定時推送
UNLocationNotificationTrigger 位置變化推送
(1)關(guān)于UNTimeIntervalNotificationTrigger 通知延時設(shè)置躏尉,注意 :如果 repeats 設(shè)置為YES 則延時時間必須大于 60s 否則會 crash;
(2)UNCalendarNotificationTrigger 使用定時推送 蚯根,通過類NSDateComponents 來設(shè)置具體的提醒日期;
(3)UNLocationNotificationTrigger 使用位置變化觸發(fā)推送胀糜,通過類CLRegion 來設(shè)置經(jīng)緯度颅拦;
3、通知中的代理事件
//當(dāng)應(yīng)用在前臺時教藻,收到通知會觸發(fā)這個代理方法距帅;你可以在這個方法中對應(yīng)用處于前臺時接到通知做一些自己的處理
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionAlert);
}
//當(dāng)點擊進入應(yīng)用時觸發(fā);
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionAlert);
}
這里complitionHandler中參數(shù)是UNNotificationPresentationOptions 通知提醒的方式 :
UNNotificationPresentationOptionBadge ,
UNNotificationPresentationOptionSound ,
UNNotificationPresentationOptionAlert ,
歡迎大家 看過后括堤,對文章做出評價碌秸,與指導(dǎo)绍移!關(guān)于推送的更多內(nèi)容我會不斷的完善更新。