iOS10通知(六)--Notification Service Extension

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、代碼實現

- (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];
}
3、多媒體遠程推送的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"
}
4、遠程推送消息,然後用拓展修改消息內容,payload如下

{
  "aps":{
    "alert":{
      "title":"多媒體通知",
      "body":"發送了一個美女的圖片"
    },
    "mutable-content":1,
    "category":"modify"
  }
}
效果圖如下





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章