代码地址如下:
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 *)urlStr

withType:(NSString *)type

completionHandle:(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:attachmentURL

completionHandler:^(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. iOS开发 iOS10推送必看

    iOS10更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说.希望看完我的这篇文章,对大家有所帮助. 一.简单入门篇---看完就可以简单适配完了 相对简单的推送证书以及环境的问题,我就不在这 ...

  2. iOS的推送机制APNs:本地推送&远程推送

    本地推送: 本地推送主要应用在备忘录,闹钟等本地的,基于时间定时的消息提醒.本篇不做详细描述. 远程推送:APNS(苹果推送通知服务) iOS远程推送机制的原理及流程: 注册推送(橙色部分):若该Ap ...

  3. iOS 消息推送(APNs) 傻瓜式教程

    也可以去我的简书页面查看这篇文章 首先: 1.做iOS消息推送需要真机测试 2.做iOS消息推送需要有付费的开发者账号 是否继续看帖? 先学习一下相关的知识吧! 因为中途可能会遇到一些问题,这篇文章或 ...

  4. iOS10推送必看UNNotificationServiceExtension

    转:http://www.cocoachina.com/ios/20161017/17769.html (收录供个人学习用) iOS10推送UNNotificationServic 招聘信息: 产品经 ...

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

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

  6. iOS10 推送通知 UserNotifications

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

  7. IOS 推送消息 php做推送服务端

    IOS推送消息是许多IOS应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPh ...

  8. .NET向APNS苹果消息推送通知

    一.Apns简介: Apns是苹果推送通知服务. 二.原理: APNs会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书以确定其为苹果设备):然后,将服务器的信息接收并且 ...

  9. Easy APNs Provider 消息推送测试工具

    1.Easy APNs Provider 简介 Easy APNs Provider 是一款为 iOS.Mac App 提供推送测试的小工具. App Store 下载地址 Easy APNs Pro ...

随机推荐

  1. springBoot 发布war包

    1.packaging 改为war <packaging>war</packaging> 2.剔除内置tomcat <dependency> <groupId ...

  2. redis的持久化(RDB&AOF的区别)

    RDB 是什么? 在指定的时间间隔内将内存中的数据集快照写入磁盘, 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里. Redis会单独创建(fork)一个子进程来进行持久化,会 ...

  3. Feign负载均衡(五)

    一.Feign定义 Feigin是服务消费者,Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Fei ...

  4. [转载]数学【p1900】 自我数

    题目描述-->p1900 自我数 本文转自@keambar 转载已经原作者同意 分析: 思路还是比较好给出的: 用类似筛选素数的方法筛选自我数. 但是要注意到题目限制的空间仅有4M,不够开10^ ...

  5. 数据排序 第三讲( 各种排序方法 结合noi题库1.10)

    说了那么多种排序方法了,下面就来先做几个题吧 06:整数奇偶排序 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 给定10个整数的序列,要求对其重新排序.排序 ...

  6. [CodeChef-LVGFT]Lovers Gift

    题目大意: 给定一个$n(n\le10^5)$个结点的树,初始全为白点.$m(m\le10^5)$次操作,每次将点$x$染成黑色或询问从$x$出发至少经过一个黑点能到达的点中,编号次大的点. 思路: ...

  7. [POI2001]Peaceful Commission

    题目大意: 有n个国家要派代表开会,每个国家有两个代表可供选择. 有m对代表有仇,不能同时开会. 若每个国家只能派一个代表开会,问是否存在一种方案,使得每个国家都能正常参会? 如果有,输出字典序最小的 ...

  8. [CEOI2017]One-Way Streets

    题目大意: 给你一个无向图,现在告诉你一些点对(u,v), 要你在保证从u到v的所有路径都不变的情况下,尽可能把所有的边变成单向边, 问你可以唯一确定哪些边的方向,以及方向是从u到v还是从v到u. 思 ...

  9. 1.2(Mybatis学习笔记)Mybatis核心配置

    一.Mybatis核心对象 1.1SqlSeesionFactory SqlSessionFactory主要作用是创建时SqlSession. SqlSessionFactory可通过SqlSessi ...

  10. 1.3(学习笔记)JSP(Java Server Pages)内置对象

    一.内置对象 内置对象又称内建对象.隐式对象,是由服务器自动创建实例化的, 用户在使用时不需要显示的创建,可直接使用. jsp内置对象名称,类型及作用域 Scope代表该内置对象的作用范围,page表 ...