iOS10 UserNotificationService集成

創建Notification Service Extension

Paste_Image.png

創建的Service Extension會自動創建NotificationService文件

Paste_Image.png

具體實現的Demo就不放了,代碼也比較簡單,就是提供一些思路而已,有疑問可以留言,盡量回復

收到通知之后的處理

  1. 首先我們需要確定我們的通知可展示的素材類型,我們暫時支持png,gif,mp4三種類型的素材
#define fileNameArray @[@"notificationImage.png",@"notificationGif.gif",@"notificationVideo.mp4"]//定義的素材下載之后存儲的文件名稱
#define identifierArray @[@"ImageNotification",@"GifNotification",@"VideoNotification"]//定義通知素材的Identifier
  1. 定義enum表示通知展示的類型
typedef enum : NSUInteger {
    NotificationImageType,
    NotificationGifType,
    NotificationVideoType
} NotificationType;
  1. 通知中自定義增加的素材參數
1、imageUrl表示圖片素材
2、videoUrl表示視頻素材
3、gifUrl表示gif圖素材
4、subTitle表示通知的子標題
這些字段均在個推通知的payload中存在,根據不同的字段展示不同的素材
  1. 通知具體的處理
    這里我們以個推為例,個推通知包含payload字段,我們可以在payload字段中定義通知展示的類型,通知的主要處理在方法:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    // Modify the notification content here...
//個推userinfo中的payload解析出來是NSString類型,需要轉換成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) {
//視頻的展示最好判斷當前的網絡狀態是否為Wifi,防止用戶流量的流失,引入Reachability文件,可判斷當前網絡是否為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.存儲通知附帶的素材文件
Tips:如果應用不支持ATS,請在Extension的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];
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容