iOS10通知(五)--本地实现多媒体通知
2017-03-31 16:52
489 查看
iOS 10 中,开发者现在可以在通知中嵌入图片、音乐或者视频。
为本地通知添加多媒体内容十分简单,只需要通过文件的NSURL创建一个 UNNotificationAttachment 对象,然后将这个对象放到数组中赋值给 content 的 attachments 属性就行了
如果需要实现远程的多媒体通知,那就要用到下篇中的通知拓展,具体操作在下篇介绍,本片只介绍本地多媒体通知
多媒体通知附件的文件大小限制
1、本地多媒体通知的关键代码段实现
2、更换url地址之后可以实现图片、音乐和视频的发送,效果图如下
为本地通知添加多媒体内容十分简单,只需要通过文件的NSURL创建一个 UNNotificationAttachment 对象,然后将这个对象放到数组中赋值给 content 的 attachments 属性就行了
如果需要实现远程的多媒体通知,那就要用到下篇中的通知拓展,具体操作在下篇介绍,本片只介绍本地多媒体通知
多媒体通知附件的文件大小限制
1、本地多媒体通知的关键代码段实现
-(void)btnClicked { UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init]; content.title = @"多媒体通知"; content.body = @"显示一个图片"; //需要显示多个图片就需要用到后面介绍的自定义的UI NSString *imageUrlStr = @"http://172.20.90.117/www2/img/r8.jpg"; [self downloadAndSave:[[NSURL alloc] initWithString:imageUrlStr] handler:^(NSURL *localUrl) { UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachment" URL:localUrl options:nil error:nil]; content.attachments = @[attachment]; UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; NSString *identifier = @"media"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { // }]; }]; } -(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]; }
2、更换url地址之后可以实现图片、音乐和视频的发送,效果图如下
相关文章推荐
- iPhone实现本地通知
- IOS 实现本地通知(local notification)的yong法
- 实现手机来电铃声,通知铃声、警告铃声等音频定制化功能(三,多媒体扫描结果定制处理)
- 闹钟功能实现+本地通知+音频播放
- iOS之推送通知-本地-服务器3.服务器端实现: 如果要编写内容提供者的推送服务程序,需要进行SSL认证编程,以及构建APNS数据包,数据包分为3个主要部分:Command(命令)、deviceTo
- iOS实现本地通知(local notification)的方法
- iOS项目开发实战——实现苹果本地消息通知推送服务
- Android实现本地推送通知的解决方案
- ios-实现本地通知(UILocalNotification)
- (七十三)iOS本地推送通知的实现
- ios本地通知和远程通知(文章最后有个php服务端实现DEMO)
- cocos2dx 本地推送通知的实现
- iOS实现本地通知
- Ios开发中UILocalNotification实现本地通知实现提醒功能
- 使用本地通知UILocalNotification实现简易闹钟
- iPhone实现本地通知
- iOS实现本地通知
- ios本地通知和远程通知详解-------(注:文章最后有个php服务端实现DEMO)
- 【iOS开发】UILocalNotification 本地通知的实现 —— 伊禾媛
- (七十三)iOS本地推送通知的实现