iOS10 UserNotificationService集成

創(chuàng)建Notification Service Extension

Paste_Image.png

創(chuàng)建的Service Extension會(huì)自動(dòng)創(chuàng)建NotificationService文件

Paste_Image.png

具體實(shí)現(xiàn)的Demo就不放了,代碼也比較簡(jiǎn)單,就是提供一些思路而已,有疑問(wèn)可以留言,盡量回復(fù)

收到通知之后的處理

  1. 首先我們需要確定我們的通知可展示的素材類型,我們暫時(shí)支持png,gif,mp4三種類型的素材
#define fileNameArray @[@"notificationImage.png",@"notificationGif.gif",@"notificationVideo.mp4"]//定義的素材下載之后存儲(chǔ)的文件名稱
#define identifierArray @[@"ImageNotification",@"GifNotification",@"VideoNotification"]//定義通知素材的Identifier
  1. 定義enum表示通知展示的類型
typedef enum : NSUInteger {
    NotificationImageType,
    NotificationGifType,
    NotificationVideoType
} NotificationType;
  1. 通知中自定義增加的素材參數(shù)
1、imageUrl表示圖片素材
2、videoUrl表示視頻素材
3、gifUrl表示gif圖素材
4、subTitle表示通知的子標(biāo)題
這些字段均在個(gè)推通知的payload中存在,根據(jù)不同的字段展示不同的素材
  1. 通知具體的處理
    這里我們以個(gè)推為例,個(gè)推通知包含payload字段,我們可以在payload字段中定義通知展示的類型,通知的主要處理在方法:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    // Modify the notification content here...
//個(gè)推userinfo中的payload解析出來(lái)是NSString類型,需要轉(zhuǎn)換成NSDictionary
    NSString *payloadstr = [request.content.userInfo objectForKey:@"payload"];
    NSData *jsondata = [payloadstr dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *payload = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingMutableContainers error:nil];
    if (payload) {
        NSString *subTitle = [payload objectForKey:@"subTitle"];
        self.bestAttemptContent.subtitle = subTitle ? subTitle : @"";
        
        NSString *imageUrl = [payload objectForKey:@"imageUrl"];
        NSString *videoUrl = [payload objectForKey:@"videoUrl"];
        NSString *gifUrl = [payload objectForKey:@"gifUrl"];
        if (imageUrl && imageUrl.length !=0) {
            [self savePath:imageUrl dataType:NotificationImageType];
        } else if (videoUrl && videoUrl.length != 0) {
//視頻的展示最好判斷當(dāng)前的網(wǎng)絡(luò)狀態(tài)是否為Wifi,防止用戶流量的流失,引入Reachability文件,可判斷當(dāng)前網(wǎng)絡(luò)是否為Wifi
            if (Reachability) {//這里判斷的條件我們采用的自己封裝的類,就不貼代碼了
                [self savePath:videoUrl dataType:NotificationVideoType];
            } else {
                self.contentHandler(self.bestAttemptContent);
            }
        } else if (gifUrl && gifUrl.length != 0) {
            [self savePath:gifUrl dataType:NotificationGifType];
        }else {
            self.contentHandler(self.bestAttemptContent);
        }
    }
}

5.存儲(chǔ)通知附帶的素材文件
Tips:如果應(yīng)用不支持ATS,請(qǐng)?jiān)贓xtension的plist增加ATS的配置,否則素材下載失敗

- (void)savePath:(NSString*)path dataType:(NotificationType)type {
    NSString *templatePath = NSTemporaryDirectory();
    NSString *filePath = @"";
    if (type < fileNameArray.count) {
        filePath = [NSString stringWithFormat:@"%@/%@",templatePath,fileNameArray[type]];
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        [fileManager removeItemAtPath:filePath error:nil];
    }
    [[[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:path] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (location) {
            NSError *moveError = nil;
            [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:filePath] error:&moveError];
            if (!error) {
                UNNotificationAttachment *attachment;
                if (type < identifierArray.count) {
                    attachment = [UNNotificationAttachment attachmentWithIdentifier:identifierArray[type] URL:[NSURL fileURLWithPath:filePath] options:nil error:nil];
                }
                if (attachment) {
                    self.bestAttemptContent.attachments = @[attachment];
                }
            }
        }
        self.contentHandler(self.bestAttemptContent);
    }] resume];
}

最后編輯于
?著作權(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)容