iOS在推送中添加图片

参考链接:http://www.jb51.net/article/109466.htm
1、新建Target

iOS在推送中添加图片_第1张图片
2017324102832872.jpg

2、在新target打开通知
iOS在推送中添加图片_第2张图片
屏幕快照 2017-12-25 下午4.18.07.png

3、在新创建的target的.m里面
、、、

  • (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.contentHandler = contentHandler;
    self.bestAttemptContent = [request.content mutableCopy];
    NSDictionary * userInfo = request.content.userInfo;
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

    NSLog(@"来了远程通知--%@",userInfo);

    //服务端与客户端约定各种资源的url,根据url资源进行下载
    NSString * imageUrl = [userInfo objectForKey:@"imageUrl"];
    NSString * gifUrl = [userInfo objectForKey:@"gifUrl"];
    NSString * typeString ;
    NSURL * url;
    if (imageUrl.length>0) {
    url = [NSURL URLWithString:imageUrl];
    typeString = @"png";
    }
    if (gifUrl.length>0) {
    url = [NSURL URLWithString:gifUrl];
    typeString = @"gif";
    }
    if (url) {
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
    //注意使用DownloadTask,这点会详细说明
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:urlRequest completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (!error) {
    NSString *path = [location.path stringByAppendingString:[NSString stringWithFormat:@".%@",typeString]];
    NSError *err = nil;
    NSURL * pathUrl = [NSURL fileURLWithPath:path];
    [[NSFileManager defaultManager] moveItemAtURL:location toURL:pathUrl error:nil];
    //下载完毕生成附件,添加到内容中
    UNNotificationAttachment *resource_attachment = [UNNotificationAttachment attachmentWithIdentifier:@"attachment" URL:pathUrl options:nil error:&err];
    if (resource_attachment) {
    self.bestAttemptContent.attachments = @[resource_attachment];
    }
    //设置为@""以后,进入app将没有启动页
    self.bestAttemptContent.launchImageName = @"";
    UNNotificationSound *sound = [UNNotificationSound defaultSound];
    self.bestAttemptContent.sound = sound;
    if (error) {
    NSLog(@"%@", error);
    }else{
    //回调给系统
    self.contentHandler(self.bestAttemptContent);
    }
    }
    else{
    self.contentHandler(self.bestAttemptContent);
    }
    }];
    [task resume];
    }
    else{
    self.contentHandler(self.bestAttemptContent);
    }

    // Modify the notification content here...
    // self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    //
    // self.contentHandler(self.bestAttemptContent);
    }

  • (void)serviceExtensionTimeWillExpire {
    // Called just before the extension will be terminated by the system.
    // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
    self.contentHandler(self.bestAttemptContent);
    }
    、、、

4、推送中添加发Push的时候,加上”mutable-content”: 1
iOS在推送中添加图片_第3张图片
屏幕快照 2017-12-25 下午4.23.20.png

你可能感兴趣的:(iOS在推送中添加图片)