首先感謝喵神的活久見(jiàn)的重構(gòu) - iOS 10 UserNotifications 框架解析和霜神的WWDC2016 Session筆記 - iOS 10 推送Notification新特性员咽,文章對(duì)推送剖析的很徹底翎朱,本文僅僅是學(xué)習(xí)實(shí)踐過(guò)程中的筆記(還用的Objective-C)昂验。
發(fā)送通知
在一切開(kāi)始之前,一定要#import <UserNotifications/UserNotifications.h>
瓢娜。因?yàn)閕OS10中,推送通知的相關(guān)功能提取到了這個(gè)新加入的框架视事。
然后监憎,就可以上代碼了~
// 申請(qǐng)通知權(quán)限
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
// A Boolean value indicating whether authorization was granted. The value of this parameter is YES when authorization for the requested options was granted. The value is NO when authorization for one or more of the options is denied.
if (granted) {
// 1、創(chuàng)建通知內(nèi)容贸人,注:這里得用可變類型的UNMutableNotificationContent间景,否則內(nèi)容的屬性是只讀的
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
// 標(biāo)題
content.title = @"柯梵通知";
// 次標(biāo)題
content.subtitle = @"柯梵辦公室通知";
// 內(nèi)容
content.body = @"柯梵科技是一家以人為本,具有強(qiáng)烈社會(huì)責(zé)任感的公司艺智。公司的最大愿景就是每個(gè)員工都能住上大房子拱燃,開(kāi)上好車,實(shí)現(xiàn)逆襲高富帥力惯、白富美的愿望。";
self.badge++;
// app顯示通知數(shù)量的角標(biāo)
content.badge = @(self.badge);
// 通知的提示聲音召嘶,這里用的默認(rèn)的聲音
content.sound = [UNNotificationSound defaultSound];
NSURL *imageUrl = [[NSBundle mainBundle] URLForResource:@"jianglai" withExtension:@"jpg"];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"imageIndetifier" URL:imageUrl options:nil error:nil];
// 附件 可以是音頻父晶、圖片、視頻 這里是一張圖片
content.attachments = @[attachment];
// 標(biāo)識(shí)符
content.categoryIdentifier = @"categoryIndentifier";
// 2弄跌、創(chuàng)建通知觸發(fā)
/* 觸發(fā)器分三種:
UNTimeIntervalNotificationTrigger : 在一定時(shí)間后觸發(fā)甲喝,如果設(shè)置重復(fù)的話,timeInterval不能小于60
UNCalendarNotificationTrigger : 在某天某時(shí)觸發(fā)铛只,可重復(fù)
UNLocationNotificationTrigger : 進(jìn)入或離開(kāi)某個(gè)地理區(qū)域時(shí)觸發(fā)
*/
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
// 3埠胖、創(chuàng)建通知請(qǐng)求
UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:@"KFGroupNotification" content:content trigger:trigger];
// 4、將請(qǐng)求加入通知中心
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
if (error == nil) {
NSLog(@"已成功加推送%@",notificationRequest.identifier);
}
}];
}
}];
這樣淳玩,發(fā)送通知的步驟就完成了直撤,回到手機(jī)桌面(不特別設(shè)置的話,應(yīng)用內(nèi)是不展示通知的)蜕着,5秒之后就可以收到一條通知了谋竖。如下圖(解鎖狀態(tài)和鎖屏狀態(tài)):
要注意的是红柱,一旦用戶拒絕了這個(gè)請(qǐng)求,再次調(diào)用該方法也不會(huì)再進(jìn)行彈窗蓖乘,想要應(yīng)用有機(jī)會(huì)接收到通知的話锤悄,用戶必須自行前往系統(tǒng)的設(shè)置中為你的應(yīng)用打開(kāi)通知,而這往往是不可能的嘉抒。因此零聚,在合適的時(shí)候彈出請(qǐng)求窗,在請(qǐng)求權(quán)限前預(yù)先進(jìn)行說(shuō)明些侍,而不是直接粗暴地在啟動(dòng)的時(shí)候就進(jìn)行彈窗隶症,會(huì)是更明智的選擇。
這里我和瞄神的觀點(diǎn)一致娩梨,所以在需要發(fā)送通知的地方才申請(qǐng)通知權(quán)限沿腰。
修改通知
修改內(nèi)容,增加一個(gè)新的通知請(qǐng)求到消息中心狈定,此請(qǐng)求和原來(lái)的標(biāo)識(shí)符一樣颂龙。
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
......
UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:@"KFGroupNotification" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
if (error == nil) {
NSLog(@"已成功更新推送%@",notificationRequest.identifier);
}
}];
這樣收到的通知就變成修改之后的了。
取消通知
取消通知分兩種纽什,取消未發(fā)送的通知和移除已展示過(guò)的通知措嵌。
// 移除已展示過(guò)的通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
// 取消還未發(fā)送的通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
添加Action
要發(fā)送一個(gè)帶有action的通知,先注冊(cè)category芦缰。
// 輸入動(dòng)作
UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"inputIndentifier" title:@"輸入" options:UNNotificationActionOptionForeground textInputButtonTitle:@"回復(fù)" textInputPlaceholder:@"請(qǐng)回復(fù)"];
// 接受動(dòng)作
UNNotificationAction *commitAction = [UNNotificationAction actionWithIdentifier:@"commitIndentifier" title:@"確定" options:UNNotificationActionOptionForeground];
// 取消動(dòng)作
UNNotificationAction *cancelAction = [UNNotificationAction actionWithIdentifier:@"cancelIndentifier" title:@"取消" options:UNNotificationActionOptionDestructive];
// 自定義類型
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"categoryIndentifier" actions:@[inputAction,commitAction,cancelAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *set = [[NSSet alloc] initWithObjects:category, nil];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:set];
這里可以在程序啟動(dòng)的時(shí)候注冊(cè)企巢,后面發(fā)通知直接根據(jù)不同的標(biāo)識(shí)符選擇不同的category即可。
content.categoryIdentifier = @"categoryIndentifier";
這時(shí)候發(fā)的通知如下圖让蕾,多了三個(gè)按鈕浪规,點(diǎn)第一個(gè)輸入按鈕還能直接輸入內(nèi)容發(fā)送:
處理通知
UNUserNotificationCenterDelegate
提供的兩個(gè)代理方法,第一個(gè)方法主要用來(lái)決定是否在應(yīng)用內(nèi)展示通知探孝。
// 如果在應(yīng)用內(nèi)展示通知 (如果不想在應(yīng)用內(nèi)展示笋婿,可以不實(shí)現(xiàn)這個(gè)方法)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
// 展示
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
// // 不展示
// completionHandler(UNNotificationPresentationOptionNone);
}
第二個(gè)方法用來(lái)處理用戶與推送的通知進(jìn)行的交互。
// 對(duì)通知進(jìn)行響應(yīng)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
// 根據(jù)類別標(biāo)識(shí)符處理目標(biāo)反應(yīng)
if ([response.notification.request.content.categoryIdentifier isEqualToString:@"categoryIndentifier"]) {
[self handleResponse:response];
}
completionHandler();
}
- (void)handleResponse:(UNNotificationResponse *)response {
NSString *actionIndentifier = response.actionIdentifier;
// 處理留言
if ([actionIndentifier isEqualToString:@"inputIndentifier"]) {
UNTextInputNotificationResponse *input = (UNTextInputNotificationResponse *)response;
NSLog(@"%@",input.userText);
UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
navVC.visibleViewController.view.backgroundColor = [UIColor redColor];
}
// 處理確定點(diǎn)擊事件
else if ([actionIndentifier isEqualToString:@"commitIndentifier"]) {
NSLog(@"用戶點(diǎn)確定了~~");
UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
navVC.visibleViewController.view.backgroundColor = [UIColor greenColor];
}
// 處理取消點(diǎn)擊事件
else {
NSLog(@"用戶點(diǎn)取消了~~");
UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;
navVC.visibleViewController.view.backgroundColor = [UIColor yellowColor];
}
}
當(dāng)然顿颅,要實(shí)現(xiàn)代理方法缸濒,前提得設(shè)置代理。這里我直接把KFADelegate
設(shè)置為消息中心的代理粱腻。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setNotificationCategorys];
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
return YES;
}
因?yàn)樯婕暗酱蜷_(kāi)應(yīng)用的行為庇配,所以實(shí)現(xiàn)了這個(gè)方法的 delegate 必須在 applicationDidFinishLaunching:
返回前就完成設(shè)置
自定義樣式
新建一個(gè)Notification Content Extention,配置info.plist文件
然后在
- (void)didReceiveNotification:(UNNotification *)notification
里取出通知的內(nèi)容并賦給UI控件展示绍些。
- (void)didReceiveNotification:(UNNotification *)notification {
// 標(biāo)題
self.titleLbl.text = notification.request.content.title;
// 次標(biāo)題
self.subLbl.text = notification.request.content.subtitle;
// 內(nèi)容
self.label.text = notification.request.content.body;
UNNotificationAttachment *attachment = notification.request.content.attachments.firstObject;
if (attachment.URL.startAccessingSecurityScopedResource) {
// 圖片
UIImage *temptImage = [[UIImage alloc] initWithContentsOfFile:attachment.URL.path];
// 壓縮避免圖片過(guò)大展示不全
NSData *imageData = UIImageJPEGRepresentation(temptImage, 1.0);
UIImage *resultImage = [[UIImage alloc] initWithData:imageData];
self.img.image = resultImage;
[attachment.URL stopAccessingSecurityScopedResource];
}
}
效果:
待解決:
1捞慌、圖片未加載全。(此問(wèn)題經(jīng)小巖同學(xué)的提醒柬批,已解決卿闹,效果見(jiàn)上圖)
2揭糕、布局不夠漂亮。
TODO
到這里基于iOS10的本地推送基本上就實(shí)現(xiàn)了锻霎,還有些東西還沒(méi)嘗試著角,比如推送音頻和視頻······