iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐
前言
我们首先要在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;
- 在上面的下载多媒体文件的- (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推送图片、视频、音乐
注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权
iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐的更多相关文章
- iOS开发 iOS10推送必看
iOS10更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说.希望看完我的这篇文章,对大家有所帮助. 一.简单入门篇---看完就可以简单适配完了 相对简单的推送证书以及环境的问题,我就不在这 ...
- iOS的推送机制APNs:本地推送&远程推送
本地推送: 本地推送主要应用在备忘录,闹钟等本地的,基于时间定时的消息提醒.本篇不做详细描述. 远程推送:APNS(苹果推送通知服务) iOS远程推送机制的原理及流程: 注册推送(橙色部分):若该Ap ...
- iOS 消息推送(APNs) 傻瓜式教程
也可以去我的简书页面查看这篇文章 首先: 1.做iOS消息推送需要真机测试 2.做iOS消息推送需要有付费的开发者账号 是否继续看帖? 先学习一下相关的知识吧! 因为中途可能会遇到一些问题,这篇文章或 ...
- iOS10推送必看UNNotificationServiceExtension
转:http://www.cocoachina.com/ios/20161017/17769.html (收录供个人学习用) iOS10推送UNNotificationServic 招聘信息: 产品经 ...
- iOS10 推送通知详解(UserNotifications)
iOS10新增加了一个UserNotificationKit(用户通知框架)来整合通知相关的API,UserNotificationKit框架增加了很多令人惊喜的特性: 更加丰富的推送内容:现在可以设 ...
- iOS10 推送通知 UserNotifications
简介 新框架 获取权限 获取用户设置 注册APNS,获取deviceToken 本地推送流程 远程推送流程 通知策略(Category+Action) 附件通知 代理回调 简介 iOS10新增了Use ...
- IOS 推送消息 php做推送服务端
IOS推送消息是许多IOS应用都具备的功能,最近也在研究这个功能,参考了很多资料终于搞定了,下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPh ...
- .NET向APNS苹果消息推送通知
一.Apns简介: Apns是苹果推送通知服务. 二.原理: APNs会对用户进行物理连接认证,和设备令牌认证(简言之就是苹果的服务器检查设备里的证书以确定其为苹果设备):然后,将服务器的信息接收并且 ...
- Easy APNs Provider 消息推送测试工具
1.Easy APNs Provider 简介 Easy APNs Provider 是一款为 iOS.Mac App 提供推送测试的小工具. App Store 下载地址 Easy APNs Pro ...
随机推荐
- php7使用curl
/** * @param string $url * @return mixed */ public function doGet($url) { //初始化 $ch = curl_init(); c ...
- AC日记——[JSOI2008]火星人prefix bzoj 1014
1014 思路: 平衡树+二分答案+hash: 好了懂了吧. 代码: #include <cstdio> #include <cstring> #include <ios ...
- windows上的命令telnet
telnet可以测试端口号是否可用,比如: telnet ip:port 或者 telnet www.baidu.com PS:win7环境下,默认没有安装telnet客户端,你可以去“控制面板”-- ...
- (1)Maven安装及配置
一.下载 http://maven.apache.org/download.cgi 解压 二.配置环境变量 1 2.path mvn -version 三.本地仓库 本地仓库用来存放远程下载的包,默 ...
- 百度之星资格赛 2016 Problem 1002
本文链接:http://www.cnblogs.com/Ash-ly/p/5494623.html 题意: 度熊面前有一个全是由1构成的字符串,被称为全1序列.你可以合并任意相邻的两个1,从而形成一个 ...
- java 时间格式化中的模式字母
java日期格式化中的模式字母有特定的意义,由于没有注意,今天在做工程的时候导致出现了奇怪的日期: 错误写法如下: 然而得到了错误的结果: 正确写法如下: 这样就得到了正确的结果 mm指的是分钟,MM ...
- POJ 1722 SUBTRACT
给定一个数组a[1,2,..,n] .定义数组第i位上的减操作:把ai和ai+1换成ai - ai+1.输入一个n位数组以及目标整数t,求一个n-1次操作序列,使得最后剩下的数等于t最后输出依此操作的 ...
- spoj - Longest Common Substring(后缀自动机模板题)
Longest Common Substring 题意 求两个串的最长公共子串. 分析 第一个串建后缀自动机,第二个串在自动机上跑,对于自动机上的结点(状态)而言,它所代表的最大长度为根结点到当前结点 ...
- 洛谷——P3913 车的攻击
P3913 车的攻击 题目描述 N \times NN×N 的国际象棋棋盘上有KK 个车,第ii个车位于第R_iRi行,第C_iCi 列.求至少被一个车攻击的格子数量. 车可以攻击所有同一行或者同 ...
- 37、Django实战第37天:404以及500页面配置
1.把404.html,500.html复制到templates下,替换静态文件路径 2.编辑users.views.py定义404,505函数 from django.shortcuts impor ...