iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐 之Service Extension 无法接受到极光网页端发出的mutable-content推送

项目需要在APP通知中添加右侧小图标,如图:此功能在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;

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下载地址 ,如果有错误欢迎大家指出,谢谢。

* 没有结束呢!!!注意事项 *
这是网友的分享,在最后我再添加自己遇到的两个坑:(极光推送,不过推送本质一样)
第一点:推送的时候有没有传mutable-content这个字段,在aps里修改此字段,
“mutable-content”: 1,代表自定义了推送 例如图片、视频、声音等
“content-available”:1,静默推送
不修改此字段默认为content-available,如图极光推送,请注意aps参数:

推送通知userInfo {
    "_j_business" = 1;
    "_j_msgid" = 58546795339529040;
    "_j_uid" = 18045808961;
    aps =     {
        alert =         {
            body = "客户在线预约了您咨询项目,请您尽快回复";
            title = "您有一条预约消息,请查看";
        };
        badge = 2;
        "content-available" = 1;//默认推送(使用新特性更改为:mutable-content)
        sound = default;
    };
    badgeA = 0;
    content = 666;
    imagePath = "http://180.76.239.11:8093/arsmo/images/arsmologo.png";
    isWebview = 0;
    msgId = 15325952433088006;
    type = 5;
    url = "http://120.27.163.3:8081/beauty/jsp/appspring/index.jsp";
}

新特性通知更改完之后的推送参数,请注意aps参数:

{
    "_j_business" = 1;
    "_j_msgid" = 36028798645202969;
    "_j_uid" = 18519121549;
    aps =     {
        alert =         {
            body = "客户在线预约了您咨询项目,请您尽快回复";
            title = "您有一条预约消息,请查看";
        };
        badge = 2;
        "mutable-content" = 1;//新特性使用的参数
        sound = default;
    };
    badgeA = 0;
    content = 666;
    imagePath = "http://180.76.239.11:8093/arsmo/images/arsmologo.png";
    isWebview = 0;
    msgId = 15326546237195427;
    type = 5;
    url = "http://120.27.163.3:8081/beauty/jsp/appspring/index.jsp";
}

第二点:调试(Debug)注意事项即:Service Extension 无法接受到极光网页端发出的mutable-content推送
具体操作为:Debug -> Attach to Process -> 这一步选择你的主app 看看
官方解释点击这里

猜你喜欢

转载自blog.csdn.net/lining1041204250/article/details/81235447