美文网首页
推送支持图片

推送支持图片

作者: huicuihui | 来源:发表于2018-02-27 17:11 被阅读25次

新建一个UNNotificationServiceExtension
在菜单栏下选择File-->New-->Target


image.png

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


image.png
然后写名字,点击Finish
image.png
点击Finish之后出来下面弹窗, 点击Activate。
image.png

此时我们的目录结构里面,已经多出了一个文件夹了。


image.png
到这一步,我们就新建了一个服务通知类的扩展。

下面开始实现自定义通知内容样式:

1、处理通知内容重写的方法:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    
    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    
//    self.contentHandler(self.bestAttemptContent);
    
    
    
    // 重写一些东西
    self.bestAttemptContent.title = @"我是标题";
    self.bestAttemptContent.subtitle = @"我是子标题";
    self.bestAttemptContent.body = @"我是body";
    
    // 附件
    NSDictionary *dict =  self.bestAttemptContent.userInfo;
    NSDictionary *notiDict = dict[@"aps"];

    //重新获取后台推送的内容,再设置一遍。
    NSDictionary *alertDict = notiDict[@"alert"];
    self.bestAttemptContent.title = alertDict[@"title"];

    NSString *imgUrl = [NSString stringWithFormat:@"%@",notiDict[@"imageAbsoluteString"]];
    
    if (!imgUrl.length) {
        self.contentHandler(self.bestAttemptContent);
    }
    
    [self loadAttachmentForUrlString:imgUrl withType:@"image" completionHandle:^(UNNotificationAttachment *attach) {
        
        if (attach) {
            self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];
        }
        self.contentHandler(self.bestAttemptContent);
        
    }];
    
}

2、下载附件通知的方法:

- (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];
    
}

3、判断文件类型的方法

- (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];
}

补充一些问题:

调试这个通知扩展类,为什么我跑程序的时候,打断点无反应?
答:这是因为你跑的target不对,正确的做法是,跑正确的target,具体如下图:


image.png

这个时候,再打断点,去测试发现还是不走方法。
查找资料之后发现可能是推送的内容格式不对。
推送内容格式如下:

{
    "aps": {
        "alert": "This is some fancy message.",
        "badge": 1,
        "sound": "default",
        "mutable-content": "1",
        "imageAbsoluteString": "http://upload.univs.cn/2012/0104/1325645511371.jpg"
        
    }
}

这里我们要注意一定要有"mutable-content": "1",以及一定要有Alert的字段,否则可能会拦截通知失败。(苹果文档说的)。除此之外,我们还可以添加自定义字段,比如,图片地址,图片类型。
在极光后台设置如下图参数(必须选中下面的mutable-content,否则通知消息不会变化):


image.png

结果还是不走方法断点。
继续查找原因。
选中工程---> UNNotificationServiceExtension所对应的Target-->Deploy Target设置为iOS10,因为是从iOS10才支持推送多媒体文件,它默认是从当前Xocde支持的最高版本,比如测试的手机版本iOS10.0.2,它默认是iOS 10.2.0.刚开始没有修改,推送死活出不来图片,只有文字;后台检查才发现这里是从iOS 10.2.0支持的。


image.png

还是不行,最后把target改为了项目本身的target,终于成功了,修改了推送过来消息的样式。(断点还是没走)。

相关文章

网友评论

      本文标题:推送支持图片

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