iOS10通知擴(kuò)展之Notification Service Extension

iOS 10增加了兩種新的通知擴(kuò)展:Notification Service ExtensionNotification Content Extension,本文只介紹前者(通知服務(wù)擴(kuò)展)。想要接入使用通知服務(wù)擴(kuò)展,需確保App已經(jīng)具備了接收push的能力,如果有需求,可以參考iOS Push從0到1文章

1、Notification Service Extension

通知服務(wù)擴(kuò)展(Notification Service Extension),在通知顯示給用戶之前會(huì)給你時(shí)間處理通知payloads,例如將要展示在通知中的image、GIF、Video、解密文本下載下來(lái)。

1.1 創(chuàng)建Service Extension

使用Xcode打開(kāi)項(xiàng)目,選中File -> New -> Target...,在出現(xiàn)的彈窗中選擇Notification Service Extension模板。如下圖所示:

notificationService_0.png

點(diǎn)擊Next后,你需要填寫(xiě)特定于應(yīng)用程序的相關(guān)信息。添加完畢,點(diǎn)擊Finish可以在項(xiàng)目的TARGETS里看到多了Service Extension一項(xiàng)。如圖所示:

notificationService_1.png

而項(xiàng)目中則會(huì)生成NotificationService文件夾,以及相應(yīng)的類(lèi)文件和plist文件,如圖所示:

notificationService_2.png

1.2 Extension LifeCycle

一旦你給App配置了通知服務(wù)擴(kuò)展程序后,每個(gè)通知都會(huì)執(zhí)行以下過(guò)程:

  1. App收到通知。
  2. 系統(tǒng)創(chuàng)建擴(kuò)展類(lèi)的實(shí)例對(duì)象并在后臺(tái)啟動(dòng)它。
  3. 你的擴(kuò)展程序會(huì)執(zhí)行內(nèi)容編輯和/或下載某些內(nèi)容操作。
  4. 如果你的擴(kuò)展程序執(zhí)行太長(zhǎng)時(shí)間而不能完成它的工作,將會(huì)收到通知并被立即終止。
  5. 通知顯示給用戶。

1.3 Extension Code

NotificationService類(lèi)文件中有兩個(gè)回調(diào)方法,方法如下:

// 重寫(xiě)此方法以實(shí)現(xiàn)推送通知的修改
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler;
// 擴(kuò)展程序被系統(tǒng)終止之前會(huì)被調(diào)用,你可以選擇是否覆蓋此方法
- (void)serviceExtensionTimeWillExpire;

系統(tǒng)受到通知后,在有限的時(shí)間(不超過(guò)30s)內(nèi),你的擴(kuò)展程序可以在didReceiveNotificationRequest:withContentHandler:方法內(nèi)對(duì)通知做相應(yīng)的更改并執(zhí)行contentHandler代碼塊。如果你沒(méi)有及時(shí)執(zhí)行,系統(tǒng)將會(huì)調(diào)用上面的第二個(gè)方法serviceExtensionTimeWillExpire,在這里給你提供最后一次執(zhí)行contentHandler代碼塊的機(jī)會(huì)。如果你什么都沒(méi)做,系統(tǒng)將向用戶顯示通知的原始內(nèi)容,你做的所有修改都不會(huì)生效。

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    // 根據(jù)約定好的key獲取image地址
    NSString *imageUrlString = [request.content.userInfo objectForKey:@"url_key"];
    if (![imageUrlString isKindOfClass:[NSString class]]) {
        return;
    }
    
    NSURL *url = [NSURL URLWithString:imageUrlString];
    if (!url)
        return;
    
    [[[NSURLSession sharedSession] downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            NSString *tempDict = NSTemporaryDirectory();
            
            NSString *filenameSuffix = response.suggestedFilename ? response.suggestedFilename : [response.URL.absoluteString lastPathComponent];
            NSString *attachmentID = [[[NSUUID UUID] UUIDString] stringByAppendingString:filenameSuffix];
            NSString *tempFilePath = [tempDict stringByAppendingPathComponent:attachmentID];
            
            if ([[NSFileManager defaultManager] moveItemAtPath:location.path toPath:tempFilePath error:&error]) {
                UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:attachmentID URL:[NSURL fileURLWithPath:tempFilePath] options:nil error:&error];
                
                if (!attachment) {
                    NSLog(@"Create attachment error: %@", error);
                } else {
                    _bestAttemptContent.attachments = [_bestAttemptContent.attachments arrayByAddingObject:attachment];
                }
            } else {
                NSLog(@"Move file error: %@", error);
            }
        } else {
            NSLog(@"Download file error: %@", error);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.contentHandler(self.bestAttemptContent);
        });
    }] resume];
}

2、Push payload

簡(jiǎn)單看一下push payload的格式:

{
    "aps":{
        "alert":{
            "title":"Push Title",
            "subtitle":"Push Subtitle",
            "body":"Push Body"
        },
        "sound":"default",
        "badge":1,
        "mutable-content":1
    },
    "landing_page":"https://www.baidu.com"
}

注意:

  • 如果要支持?jǐn)U展服務(wù),一定要確保payloadaps字典中包含mutable-content字段,值為1。

3、Push開(kāi)關(guān)是否打開(kāi)

Push功能完成后,我們一般會(huì)有判斷App是否打開(kāi)了通知開(kāi)關(guān)的需求。如果用戶沒(méi)有打開(kāi)可以提示用戶再次打開(kāi),以保證Push消息能夠推動(dòng)給更多的用戶,提高消息轉(zhuǎn)化率。由于iOS10以上的通知相關(guān)API發(fā)生了較大變化,我們需要針對(duì)不同的系統(tǒng)版本使用不同的API來(lái)判斷。具體代碼如下:

+ (BOOL)isOpenNotificationSetting {
    __block BOOL isOpen = NO;
    if (@available(iOS 10.0, *)) { //iOS10及iOS10以上系統(tǒng)
       dispatch_semaphore_t semaphore;
       semaphore = dispatch_semaphore_create(0);
       // 異步方法,使用信號(hào)量的方式加鎖保證能夠同步拿到返回值
       [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
           if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
               isOpen = YES;
           }
           dispatch_semaphore_signal(semaphore);
       }];
       dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
   } else { 
       UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
       if (settings.types != UIUserNotificationTypeNone) {
           isOpen = YES;
       }
   }
   return isOpen;
}

由于getNotificationSettingsWithCompletionHandler:方法是一個(gè)異步方法,如果直接在回調(diào)中去判斷當(dāng)前的push授權(quán)狀態(tài)的話,還未給isOpen賦值就已經(jīng)return返回結(jié)果了。

問(wèn)題有了,那么解決方案也有很多,如代碼中所示,我們使用了信號(hào)量dispatch_semaphore,功能類(lèi)似于iOS開(kāi)發(fā)中的鎖(比如NSLock,@synchronize等),它是一種基于計(jì)數(shù)器的多線程同步機(jī)制。

  1. 使用dispatch_semaphore_create創(chuàng)建信號(hào)量semaphore,此時(shí)信號(hào)量的值是0。
  2. 異步跳過(guò)block塊代碼,執(zhí)行dispatch_semaphore_wait,信號(hào)量的值減1。此時(shí)當(dāng)前線程處于阻塞狀態(tài),等待信號(hào)量加1才能繼續(xù)執(zhí)行。
  3. block塊代碼異步執(zhí)行,dispatch_semaphore_signal執(zhí)行后發(fā)送一個(gè)信號(hào),信號(hào)量的值加1,線程被喚醒,繼續(xù)執(zhí)行,給isOpen賦值,然后return,保證了每次能夠正確取到當(dāng)前的push開(kāi)關(guān)狀態(tài)。

4、相關(guān)配置

  1. 如果是一個(gè)新App,你需要?jiǎng)?chuàng)建2個(gè)App ID,一個(gè)用于App本身,一個(gè)用于創(chuàng)建通知服務(wù)擴(kuò)展;如果App已經(jīng)存在,再增加通知服務(wù)擴(kuò)展,只需要?jiǎng)?chuàng)建用于擴(kuò)展程序的App ID。
  2. 與1同樣,還需要?jiǎng)?chuàng)建用于擴(kuò)展程序的Provisioning Profile。
  3. 創(chuàng)建完成再Xcode完成相應(yīng)的配置。

5、推送測(cè)試工具Pusher

iOS App的推送消息測(cè)試是比較麻煩的,Pusher提供了相當(dāng)完善的push推送功能,可以幫助我們很好的完成push調(diào)試。

notificationService_3.png
  1. 選擇對(duì)應(yīng)的p12推送證書(shū)
  2. Should use sandbox environment選項(xiàng)代表你要發(fā)送push的環(huán)境是開(kāi)發(fā)環(huán)境還是生產(chǎn)環(huán)境
  3. 第三行是要輸入你當(dāng)前要發(fā)送push設(shè)備的device token
  4. 最下面的輸入框是push payload,將編輯好的json文本粘貼到這點(diǎn)擊Push推送即可

最后附上效果圖。

notificationService_4.png
notificationService_5.png

參考

iOS 10: Notification Service Extensions

iOS推送通知及推送擴(kuò)展

NWPusher Github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容