代码地址如下:
http://www.demodashi.com/demo/13208.html

前言

我们首先要在AppDelegate里面进行iOS的适配,可以参考这篇文章

iOS原生推送(APNS)的实现,
如果已经适配过了请忽略。

程序实现

Xcode打开项目,File-->New-->Target;

然后分别选UNNotificationServiceExtension、UNNotificationContent创建Target;

然后在UNNotificationServiceExtension的- (void)didReceiveNotificationRequest:(UNNotificationRequest )request withContentHandler:(void (^)(UNNotificationContent _Nonnull))contentHandler {}方法中添加下面的代码;

NSMutableArray *actionMutableArr = [[NSMutableArray alloc] initWithCapacity:1];UNNotificationAction * actionA  =[UNNotificationAction actionWithIdentifier:@"ActionA" title:@"不感兴趣" options:UNNotificationActionOptionAuthenticationRequired];UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"ActionB" title:@"不感兴趣" options:UNNotificationActionOptionDestructive];UNNotificationAction * actionC = [UNNotificationAction actionWithIdentifier:@"ActionC" title:@"进去瞅瞅" options:UNNotificationActionOptionForeground];UNTextInputNotificationAction * actionD = [UNTextInputNotificationAction actionWithIdentifier:@"ActionD" title:@"作出评论" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"send" textInputPlaceholder:@"say some thing"];[actionMutableArr addObjectsFromArray:@[actionA,actionB,actionC,actionD]];if (actionMutableArr.count) {UNNotificationCategory * notficationCategory = [UNNotificationCategory categoryWithIdentifier:@"categoryNoOperationAction" actions:actionMutableArr intentIdentifiers:@[@"ActionA",@"ActionB",@"ActionC",@"ActionD"] options:UNNotificationCategoryOptionCustomDismissAction];[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:notficationCategory]];}

上面的方法是添加推送消息下面的事件(进入应用查看,取消查看,快捷回复)的,如果你的应用不需要可以忽略;

self.contentHandler = contentHandler;self.bestAttemptContent = [request.content mutableCopy];self.bestAttemptContent.categoryIdentifier = @"categoryNoOperationAction";// Modify the notification content here...//    self.bestAttemptContent.title = [NSString stringWithFormat:@"点击查看更多内容"];NSDictionary *dict =  self.bestAttemptContent.userInfo;//    NSDictionary *notiDict = dict[@"aps"];NSString *mediaUrl = [NSString stringWithFormat:@"%@",dict[@"media"][@"url"]];NSLog(@"%@",mediaUrl);if (!mediaUrl.length) {self.contentHandler(self.bestAttemptContent);}[self loadAttachmentForUrlString:mediaUrl withType:dict[@"media"][@"type"] completionHandle:^(UNNotificationAttachment *attach) {if (attach) {self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];}self.contentHandler(self.bestAttemptContent);}];
//处理视频,图片的等多媒体- (void)loadAttachmentForUrlString:(NSString *)urlStrwithType:(NSString *)typecompletionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler{__block UNNotificationAttachment *attachment = nil;NSURL *attachmentURL = [NSURL URLWithString:urlStr];NSString *fileExt = [self fileExtensionForMediaType:type];NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];[[session downloadTaskWithURL:attachmentURLcompletionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {if (error != nil) {NSLog(@"%@", error.localizedDescription);} else {NSFileManager *fileManager = [NSFileManager defaultManager];NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExt]];[fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];NSError *attachmentError = nil;attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];if (attachmentError) {NSLog(@"%@", attachmentError.localizedDescription);}}completionHandler(attachment);}] resume];}- (NSString *)fileExtensionForMediaType:(NSString *)type {NSString *ext = type;if ([type isEqualToString:@"image"]) {ext = @"jpg";}if ([type isEqualToString:@"video"]) {ext = @"mp4";}if ([type isEqualToString:@"audio"]) {ext = @"mp3";}return [@"." stringByAppendingString:ext];}

上面的这两段代码是当推送消息来了后,我们将mdeia下的url内的文件下载到本地,然后将路径交给系统,进而实现推送多媒体文件的目的;

这里说一下必须注意的两个坑、个坑、坑:

1.将UNNotificationServiceExtension中的pilst文件中添加

(1)在Info.plist中添加NSAppTransportSecurity类型Dictionary。

(2)在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

这是因为从iOS9开始苹果不允许直接http访问,加上这两个字段就可以访问http,如果你不添加,只有你推送https的media文件才能被下载,http则不能被下载;

2.选中工程---> UNNotificationServiceExtension所对应的Target-->Deploy Target设置为iOS10,因为是从iOS10才支持推送多媒体文件,它默认是从当前Xocde支持的最高版本,比如小编的手机版本iOS10.0.2,它默认是iOS 10.2.0.刚开始小编没有修改,推送死活出不来图片,只有文字;后台检查才发现这里是从iOS 10.2.0支持的,心中一万个草泥马崩腾而过;

忙活了这么多还是看一下效果吧,我只做了图片与视频,声音应该差别不大。

推送视频效果图

推送图片效果图

自定义推送的UI

1、在UNNotificationServiceExtension的- (void)didReceiveNotificationRequest:(UNNotificationRequest )request withContentHandler:(void (^)(UNNotificationContent _Nonnull))contentHandler方法内为self.bestAttemptContent添加categoryIdentifier

self.bestAttemptContent.categoryIdentifier = @"myNotificationCategory";

然后将这个categoryIdentifier粘贴在UNNotificationContent的infoplist内NSExtension-->NSExtensionAttributes-->UNNotificationExtensionCategory的字段内;然后在下面在添加一个字段UNNotificationExtensionDefaultContentHidden设置bool值为YES,这里是隐藏系统的UI;

  1. 在上面的下载多媒体文件的- (void)loadAttachmentForUrlString:(NSString )urlStr withType:(NSString )type completionHandle:(void(^(UNNotificationAttachment *attach))completionHandler;方法内添加
NSMutableDictionary * dict = [self.bestAttemptContent.userInfo mutableCopy];[dict setObject:[NSData dataWithContentsOfURL:localURL] forKey:@"image"];self.bestAttemptContent.userInfo = dict;

我这里是将图片的Data数据放到推送的userInfo里面,然后在自定义UI的UNNotificationContent内获取这个Date,然后加载UI;
3、 在UNNotificationContent的maininterface.storyboard内部将UI做好,然后在UNNotificationContent的- (void)didReceiveNotification:(UNNotification *)notification ;方法内进行UI加载。

UI运行效果

项目结构图

iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐

代码地址如下:
http://www.demodashi.com/demo/13208.html

注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐相关推荐

  1. 跨平台应用开发进阶(十一) :uni-app 实现IOS原生APP-云打包集成极光推送(JG-JPUSH)详细教程

    文章目录 一.前言 二.资源 三.集成 四.遇到的问题及解决措施 4.1 IOS开发者证书无推送权限 4.2 manifest中并没有配置push模块.但云端打包ios就是一直报Code Signin ...

  2. iOS原生推送(APNS)的实现

    参考:http://www.jianshu.com/p/9eae61bcc42e 1.前期准备工作 (1)证书的创建 进入苹果Apple Developer -> Member Center - ...

  3. android极光推送声音,【极光推送】iOS APNS 自定义铃声

    IOS APNS自定义推送铃声 是推送给苹果服务器时 将 推送的 key = sound 的value = @"custom.xxx".自定义铃声支持几种固定的格式.本人只以.ca ...

  4. 苹果 iOS推送 APNS(本人亲测总结)

    文章目录 1.静默推送(发透传消息)总是收不到(iOS静默通知(iOS Silent Notification):需要进一步验证 2.推送的相关方法 3.模拟推送的工具 3.1 在appstore上搜 ...

  5. 推送消息不打烊--Flutter集成个推SDK实现Android和IOS原生推送

    theme: condensed-night-purple highlight: a11y-dark 嗨!这里是甜瓜看代码,大家五一快乐呀,这篇文章跟大家聊聊怎么集成个推. Flutter集成个推SD ...

  6. iOS10 推送通知 UserNotifications

    简介 新框架 获取权限 获取用户设置 注册APNS,获取deviceToken 本地推送流程 远程推送流程 通知策略(Category+Action) 附件通知 代理回调 简介 iOS10新增了Use ...

  7. iOS10推送适配完整说明

    一年一度的iOS大版本更新又开始了,对于不明真相吃瓜群众来说真是太好啦!对于我们程序员却意味着disaster...这次的推送架构完全推翻以往,所以得从新适配,话不多说,开始吧. 1.在targets ...

  8. iOS10 推送通知详解(UserNotifications)

    iOS10新增加了一个UserNotificationKit(用户通知框架)来整合通知相关的API,UserNotificationKit框架增加了很多令人惊喜的特性: 更加丰富的推送内容:现在可以设 ...

  9. iOS 推送,删除指定推送消息或者撤回某条推送

    iOS 推送,删除指定推送消息 远程推送经常会出现收到重复推送的问题,或者想删除某条推送消息的问题,本文将详细说明 静默推送 在 iOS10 之后 Apple 新增了静默推送的功能,使 App 可以在 ...

最新文章

  1. PHP 显示文章发布日期 一小时前 一天前 一月前 一年前
  2. (笔试题)删除K位数字
  3. 万物皆可JOJO:这个GAN直接让马斯克不做人啦 | Demo可玩
  4. oracle+trunkc,Oracle常用备份与恢复操作
  5. LeetCode Ugly Number II(丑数的计算)
  6. java nio is例子,Java Buffer isDirect()用法及代码示例
  7. LetCode-MSSQL超过5名学生的课
  8. C 语言fopen打开模式
  9. Unable to find the requested .Net Framework Data Provider
  10. 计算机使用快捷键大全
  11. jtag接口_一份很好的JTAG完全解读资料
  12. fedora9 换源方法
  13. cad剪裁地形图lisp_CAD怎么在完整地形图里截取需要的部分地形图
  14. shell脚本中set -e作用
  15. oracle校验统一社会信用代码函数
  16. python中encode和decode使用讲解与演示
  17. 视频和图片合成软件,简单快速合成视频和图片
  18. 解决s3.amazonaws.com打不开、下载速度慢等问题
  19. outlook 服务器未响应,出现错误,Outlook 无法设置你的帐户
  20. canvas实现高阶贝塞尔曲线

热门文章

  1. 计算机四级数据库真题,2015年计算机四级数据库真题
  2. 将jpg文件当作php文件来解析,解析漏洞
  3. KNN算法——分类部分
  4. (一)flask-sqlalchemy的安装和配置
  5. 004-controller的使用
  6. Octave 作图 无响应
  7. [Asp.net 5] DependencyInjection项目代码分析-目录
  8. Ranger开源流水线docker化实践案例
  9. Dockerfile文件详解
  10. java Unsafe