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"
}
}
效果图如下