美文网首页ios10推送iOS推送技术iOS Developer
iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐

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

作者: liangZhen | 来源:发表于2017-03-20 17:38 被阅读1067次

    我们首先要在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;

    2. 在上面的下载多媒体文件的- (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的效果。


    这里是Demo下载地址 ,如果有错误欢迎大家指出,谢谢。

    相关文章

      网友评论

      • 點點星光:请问我下载完视频后怎么播放,我现在获取第一帧做封面直接报错没有权限打开
        點點星光:widget,刚找到是因为没有调安全访问的方法才一直无权访问
        liangZhen:@點點星光 你是在widget 播放还是进入app后播放?
      • 酷比Xcoder:大佬,请问可以推送视频吗?怎么才能在不进入APP的情况下,播放推送的视频?
        酷比Xcoder:@liangZhen 需要自己写一个播放按钮以及写播放的逻辑吗?还是系统自带有
        酷比Xcoder:@liangZhen 嗯呢,视频我推送成功了,怎么不进入APP实现播放呢?
        liangZhen:@酷比Xcoder 可以呀,这篇文章里有讲如何推送视频的。
      • 25dad3564998:这个按照您的上一个接着写 我想跳转一个其他的页面 不是首页 类似百度 是应该在这个项目中改还是需要在测试推送软件里更改
        liangZhen:@未知_9668 你可以根据后台推送过来的数据,根据不同的需求,在app内处理跳转到对应的页面就行了。
      • 白水灬煮一切:大神,按你文档走下来了,但是为什么添加的这两个target怎么不触发呢
        白水灬煮一切:@liangZhen 解决了!很感谢哈
        liangZhen:你看你的Deploy Target版本号比你的手机当前版本号低吗?就是本文中讲的第二个坑。

      本文标题:iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐

      本文链接:https://www.haomeiwen.com/subject/qxgenttx.html