美文网首页
iOS在推送中添加图片

iOS在推送中添加图片

作者: 海浪萌物 | 来源:发表于2017-12-25 16:24 被阅读935次

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

    2017324102832872.jpg
    2、在新target打开通知
    屏幕快照 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 屏幕快照 2017-12-25 下午4.23.20.png

    相关文章

      网友评论

          本文标题:iOS在推送中添加图片

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