iOS通知10.0

優(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

首先我們看一下之前的本地通知是什么樣的褐耳?

iOS10.0之前通知
//發(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];
  1. UNPushNotificationTrigger绑咱,遠程推送觸發(fā)器,一般是遠程推送推過來的通知帶有這類觸發(fā)器
  1. UNTimeIntervalNotificationTrigger窿克,時間間隔觸發(fā)器,定時或者是重復只损,在本地推送設置中有用
  2. UNCalendarNotificationTrigger,日歷觸發(fā)器爆存,指定日期進行通知
  3. UNLocationNotificationTrigger,地理位置觸發(fā)器闲勺,指定觸發(fā)通知的條件是地理位置CLRegion這個類型。
    觸發(fā)器和內容最后形成UNNotificationRequest,一個通知請求序芦,本地通知的請求,直接交給通知中心進行發(fā)送宪塔,發(fā)送成功后,該通知會按照觸發(fā)器的觸發(fā)條件進行觸發(fā)南誊,并且會顯示到通知中心上,用戶可與指定的category交互方式與通知進行交互

這里只整理了基礎知識,希望起到拋磚引玉的作用

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末蜂挪,一起剝皮案震驚了整個濱河市攒驰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖很洋,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機隧哮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門曲秉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人璧眠,你說我怎么就攤上這事。” “怎么了腰鬼?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵彼硫,是天一觀的道長词渤。 經(jīng)常有香客問我,道長沿盅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任疗锐,我火速辦了婚禮箍铲,結果婚禮上关划,老公的妹妹穿的比我還像新娘。我一直安慰自己翘瓮,他們只是感情好贮折,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著资盅,像睡著了一般调榄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上呵扛,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天振峻,我揣著相機與錄音,去河邊找鬼择份。 笑死扣孟,一個胖子當著我的面吹牛,可吹牛的內容都是我干的荣赶。 我是一名探鬼主播凤价,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鸽斟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了利诺?” 一聲冷哼從身側響起富蓄,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎慢逾,沒想到半個月后立倍,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡侣滩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年口注,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片君珠。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡寝志,死狀恐怖,靈堂內的尸體忽然破棺而出策添,到底是詐尸還是另有隱情材部,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布唯竹,位于F島的核電站乐导,受9級特大地震影響,放射性物質發(fā)生泄漏浸颓。R本人自食惡果不足惜物臂,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望猾愿。 院中可真熱鬧鹦聪,春花似錦、人聲如沸蒂秘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽姻僧。三九已至规丽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間撇贺,已是汗流浹背赌莺。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留松嘶,地道東北人艘狭。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親巢音。 傳聞我的和親對象是個殘疾皇子遵倦,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內容