iOS10通知(六)--Notification Service Extension
来源:互联网 发布:数控钻床钻孔编程范例 编辑:程序博客网 时间:2024/04/29 11:05
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" }}效果图如下
0 0
- iOS10通知(六)--Notification Service Extension
- iOS10推送通知进阶(Notification Extension)
- iOS10通知(七)--Notification Content Extension
- iOS10中的通知Notification
- IOS10 Notification推送通知(一)
- IOS10 Notification推送通知(二)
- IOS10 Notification远程推送通知(三)
- iOS10通知及通知拓展Extension使用详解
- iOS10通知及通知拓展Extension使用详解(附Demo)
- Service + Notification(通知栏)实现断点下载
- Android Service检查Wifi状态并用notification通知
- 使用android push notification service 实现即时通知
- Android Service检查Wifi状态并用notification通知
- service介绍之IntentService与Notification前台通知
- Apple Push Notification Service 苹果推送通知服务 - 翻译
- Notification通知
- Notification通知
- Notification(通知)
- Calendar类常用方法 日期间的转换 set方法有巨坑
- mybatis一级缓存(session cache)引发的血案
- TCP/IP(5)--TCP与UDP
- 《递归递推练习》C
- chown和chmod的用法
- iOS10通知(六)--Notification Service Extension
- Java基础学习笔记--继承中的构造函数
- 快速在阿里云上构建机器学习应用
- centos6.5配置hosts方法以及作用
- 文章标题
- DevExpress组件之——GridControl
- Q&A——内存管理(三)
- 欲先攻其事必先利其器 (第三方资源篇)
- oracle练习之 子查询