iOS10通知(六)--Notification Service Extension
阿新 • • 發佈:2019-02-06
iOS 10 中添加了很多拓展 extension,與通知相關的拓展 extension 有兩個:Notification Service Extension 和 Notification Content Extension。前者可以讓我們有機會在收到遠端推送的通知後,展示之前對通知內容進行修改;後者可以用來自定義通知檢視的樣式。
Notification Service Extension 現在只對遠端推送的通知有效
在推送 payload 中增加一個 mutable-content 值為 1 的項表示啟用內容修改
下面利用Notification Service Extension來實現通知內容的修改和遠端推送多媒體通知
1、建立Notification Service Extension
2、程式碼實現
3、多媒體遠端推送的payload如下,效果跟本地的一樣,就不在介紹了- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // Modify the notification content here... if([self.bestAttemptContent.categoryIdentifier isEqualToString:@"modify"]) { //如果遠端推送的payload中含有modify標識,則表示需要修改訊息內容 self.bestAttemptContent.body = [NSString stringWithFormat:@"%@,modify by NotificationService",self.bestAttemptContent.body]; self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title]; self.contentHandler(self.bestAttemptContent); } else if([self.bestAttemptContent.categoryIdentifier isEqualToString:@"media"]) { //如果遠端推送的payload中含有media標識,則表示需要處理多媒體通知,包括圖片、音樂、視訊 NSString *mediaUrlStr = [self.bestAttemptContent.userInfo objectForKey:@"mediaUrl"]; NSURL *mediaUrl = [[NSURL alloc]initWithString:mediaUrlStr]; [self downloadAndSave:mediaUrl handler:^(NSURL *localUrl) { UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachment" URL:localUrl options:nil error:nil]; self.bestAttemptContent.attachments = @[attachment]; self.contentHandler(self.bestAttemptContent); }]; } else { self.contentHandler(self.bestAttemptContent); } } -(void)downloadAndSave:(NSURL *)url handler: (void (^)(NSURL *localUrl)) handler { NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { // location是沙盒中臨時資料夾下的一個臨時url,檔案下載後會存到這個位置, //由於臨時目錄中的檔案隨時可能被刪除,建議把下載的檔案挪到需要的地方 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename]; [[NSFileManager defaultManager] moveItemAtURL:location toURL:[NSURL fileURLWithPath:path] error:nil]; handler([NSURL fileURLWithPath:path]); }]; [task resume]; }
4、遠端推送訊息,然後用拓展修改訊息內容,payload如下{ "aps":{ "alert":{ "title":"多媒體通知", "body":"傳送了一個美女的圖片" }, "mutable-content":1, "category":"media" }, "mediaUrl":"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1490937858917&di=f2c49174f5d70fc92683e3b8f2bc2f45&imgtype=0&src=http%3A%2F%2Fwww.zhiyinlady.com%2Fd%2Ffile%2Fyule%2Fbayule%2F20170221%2F20170220150816873mlfamznlthd.jpg" }
{
"aps":{
"alert":{
"title":"多媒體通知",
"body":"傳送了一個美女的圖片"
},
"mutable-content":1,
"category":"modify"
}
}
效果圖如下