優(yōu)秀是一種習慣
iOS10.0發(fā)布已經(jīng)有一段時間了践惑,一直沒有系統(tǒng)的學習總結新的變化嘶卧,只是去了解了一下芥吟,最近有時間就深入的了解一下,網(wǎng)上的文章有很多但并不是自己的所以自己寫了一下
前言
為什么是[通知]而不是[推送]
先來看一下iOS10通知相關的第一個更新點就是新加了一個框架User Notification Framework运沦,從字面翻譯來看應該翻譯成“用戶通知框架”携添,而通常大家所了解的“推送”翻譯成英文應該是“Push”烈掠,“Push”其實只是[通知]觸發(fā)的一種方式缸托,而[通知]其實是操作系統(tǒng)層面的一種UI展示俐镐。
在蘋果的官方文檔中Notification分為兩類:
Remote(遠程叼风,也就是之前所說的Push的方式)
Local(本地无宿,通知由本地的事件觸發(fā),iOS10中有三種不同的觸發(fā)‘Trigger’方式蹂午,稍后會進行詳細說明)
所以彬碱,[推送]只是[通知]的一種觸發(fā)方式
從iOS迭代更新的歷史特征中看,[通知]應該是是被蘋果作為一個重點內容來延展的堡妒。(從最初的單純展示和簡單回調搬泥,到Backgroud的支持忿檩,再后來整體的Payload的長度由256字節(jié)擴展到2K再到4K,再看這次的獨立框架還有豐富的特性更新)
來看一下更新的地方
由User Notification Framework 整合通知相關方法看特性變化
通知相關的方法由之前一直存在在UIKit Framework中到獨立出來班套,官方確實做了很多吱韭,但是也盡量做到讓開發(fā)者可以平滑的過度。
原文:
1. Familiar API with feature parity
2. Expanded content
3. Same code path for local and remote notification handling
4. Simplified delegate methods
5. Better notification management
6. In-app presentation option
7. Schedule and handle notifications in extensions
8. Notification Extensions
釋義:
1. 相同的特性使用類似的API(之前的功能API使用方法類似但是還是稍有改變)
2. 內容擴展(支持附件和展示更多內容)
3. 本地通知和遠程通知操作代碼在相同調用路徑(合并代理方法)
4. 簡化代理方法
5. 更好的通知管理(支持通知查猿规、改姨俩、刪环葵;增強本地通知管理陨晶,增加日歷與地理位置事件的觸發(fā))
6. 應用內通知展示(之前App在前臺的情況下收到通知不會UI展示)
7. 在Extensions中規(guī)劃和操作通知(使更新通知內容和刪除誤發(fā)或過期的通知內容成為可能,另一個重要場景為端到端加密)
8. 引入通知Extensions
首先我們看一下之前的本地通知是什么樣的褐耳?
//發(fā)送本地推送
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];//本次開啟立即執(zhí)行的周期
notification.repeatInterval = kCFCalendarUnitDay;//循環(huán)通知的周期
notification.alertAction = @"簽到提醒";
notification.alertBody = @"簽到";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
// iOS8到iOS9后,需要添加這個注冊,才能得到授權
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
//發(fā)送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
iOS9.0之前的交互通知
/*
identifier:行為標識符,用于調用代理方法時識別是哪種行為砰诵。
title:行為名稱。
UIUserNotificationActivationMode:即行為是否打開APP理肺。
authenticationRequired:是否需要解鎖贪薪。
destructive:這個決定按鈕顯示顏色画切,YES的話按鈕會是紅色。
behavior:點擊按鈕文字輸入,是否彈出鍵盤
*/
UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"action1";
action1.title=@"策略1行為1";
action1.activationMode = UIUserNotificationActivationModeForeground;
action1.destructive = YES;
UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"action2";
action2.title=@"策略1行為2";
action2.activationMode = UIUserNotificationActivationModeBackground;
action2.authenticationRequired = NO;
action2.destructive = NO;
action2.behavior = UIUserNotificationActionBehaviorTextInput;//點擊按鈕文字輸入,是否彈出鍵盤
UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init];
category1.identifier = @"Category1";
[category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
self.notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
self.notification.timeZone=[NSTimeZone defaultTimeZone];
self.notification.alertBody=@"測試推送的快捷回復";
self.notification.category = @"Category1";
[[UIApplication sharedApplication] scheduleLocalNotification:self.notification];
//解除本地推送
- (void)removeUILocalNotification:(NSString *)notifation userkey:(NSString*)key
{
// 獲得 UIApplication
UIApplication *app = [UIApplication sharedApplication];
//獲取本地推送數(shù)組
NSArray *localArray = [app scheduledLocalNotifications];
//聲明本地通知對象
UILocalNotification *localNotification;
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict = noti.userInfo;
if (dict) {
NSString *inKey = [dict objectForKey:notifation];
if ([inKey isEqualToString:key]) {
if (localNotification){
localNotification = nil;
}
localNotification = noti;
break;
}
}
}
//判斷是否找到已經(jīng)存在的相同key的推送
if (!localNotification) {
//不存在初始化
localNotification = [[UILocalNotification alloc] init];
}
if (localNotification) {
//不推送 取消推送
[app cancelLocalNotification:localNotification];
return;
}
}
}
下面我們看一下iOS10.0通知的變化
UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc] init];
//引入代理
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
content.title = @"iOS10通知";
content.subtitle = @"新通知學習筆記";
content.body = @"新通知變化很大,之前本地通知和遠程推送是兩個類实胸,現(xiàn)在合成一個了庐完。";
content.badge = @1;
// UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"];
// content.sound = sound;
NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"sport" ofType:@"png"];
UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];
content.attachments = @[imageAttachment];
//重復提醒,時間間隔要大于60s
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
NSString *requertIdentifier = @"RequestIdentifier";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error:%@",error);
}];
UNTextInputNotificationAction *action1 = [UNTextInputNotificationAction actionWithIdentifier:@"replyAction" title:@"文字回復" options:UNNotificationActionOptionNone];
UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"enterAction" title:@"進入應用" options:UNNotificationActionOptionForeground];
UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"cancelAction" title:@"取消" options:UNNotificationActionOptionDestructive];
UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"Categroy" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:categroy]];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
- UNPushNotificationTrigger绑咱,遠程推送觸發(fā)器,一般是遠程推送推過來的通知帶有這類觸發(fā)器
- UNTimeIntervalNotificationTrigger窿克,時間間隔觸發(fā)器,定時或者是重復只损,在本地推送設置中有用
- UNCalendarNotificationTrigger,日歷觸發(fā)器爆存,指定日期進行通知
- UNLocationNotificationTrigger,地理位置觸發(fā)器闲勺,指定觸發(fā)通知的條件是地理位置CLRegion這個類型。
觸發(fā)器和內容最后形成UNNotificationRequest,一個通知請求序芦,本地通知的請求,直接交給通知中心進行發(fā)送宪塔,發(fā)送成功后,該通知會按照觸發(fā)器的觸發(fā)條件進行觸發(fā)南誊,并且會顯示到通知中心上,用戶可與指定的category交互方式與通知進行交互
這里只整理了基礎知識,希望起到拋磚引玉的作用