如何使用react native firebase将自定义映像添加到iOS中的通知

如何使用react native firebase将自定义映像添加到iOS中的通知,firebase,react-native,react-native-firebase,Firebase,React Native,React Native Firebase,我想在firebase云消息传递中使用数据负载在通知中显示图像。该图像被指定为承载该图像的网站的url 我想做的似乎是将图像添加为附件,请参见下面的第8行。但是,除了应用程序图标外,不存在任何图像 const notification = new firebase.notifications.Notification() .setNotificationId("notification_id") .setTitle(notification.data.title)

我想在firebase云消息传递中使用数据负载在通知中显示图像。该图像被指定为承载该图像的网站的url

我想做的似乎是将图像添加为附件,请参见下面的第8行。但是,除了应用程序图标外,不存在任何图像

const notification = new firebase.notifications.Notification()
        .setNotificationId("notification_id")
        .setTitle(notification.data.title)
        .setBody(notification.data.body)
        .setData({ url: notification.data.url })
        .ios.setLaunchImage(notification.data.icon)
        .android.setBigPicture(notification.data.icon)
        .ios.addAttachment("some_id", notification.data.icon, {});

问题是没有可以帮助我的错误消息。通知将按预期显示标题和正文,但不显示图像。从我阅读的文档来看,我想做的是可能的。

简单的回答是,iOS上的react native不支持“富推送通知”,即带有图像的通知

较长的答案是,如果您添加一点swift代码,那么向react原生项目添加对映像的支持就相当简单了

解决办法: 打开您的xcode项目并转到“编辑器”->“添加目标…”。选择名为“通知服务扩展”的“应用程序扩展”

您可以随意命名它,但如果您使用CocoaPods,请确保选择了正确的项目

创建后,将
覆盖func didReceive(\uRequest:UNNotificationRequest,withContentHandler contentHandler:@escaping(UNNotificationContent)->Void)
的内容替换为:

      self.contentHandler = contentHandler
      bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
      
      // get the variables that is needed later.
      guard let bestAttemptContent = bestAttemptContent,
        let attachmentURLAsString = bestAttemptContent.userInfo["icon"] as? String, 
      // "icon" is the key for the image url in the notification. It 
      // could be named whatever you want.
        let attachmentURL = URL(string: attachmentURLAsString) else {
          return
      }
      
      // call a custom function to download the image before attaching
      // it to the notification and presenting it.
      downloadImageFrom(url: attachmentURL) { (attachment) in
        if let attachment = attachment {
          bestAttemptContent.attachments = [attachment]
          contentHandler(bestAttemptContent)
        }
        
      }
然后需要创建
downloadImageFrom
函数:

  private func downloadImageFrom(url: URL, with completionHandler: @escaping (UNNotificationAttachment?) -> Void) {
    let task = URLSession.shared.downloadTask(with: url) { (downloadedUrl, response, error) in
      
      //verify that a url exists.
      guard let downloadedUrl = downloadedUrl else {
        completionHandler(nil)
        return
      }
      
      // create a local unique filepath.
      var urlPath = URL(fileURLWithPath: NSTemporaryDirectory())
      let uniqueURLEnding = ProcessInfo.processInfo.globallyUniqueString + ".png"
      urlPath = urlPath.appendingPathComponent(uniqueURLEnding)
      
      // fetch the image from the url
      try? FileManager.default.moveItem(at: downloadedUrl, to: urlPath)
      
      // if successful, return the image as an attachment.
      do {
        let attachment = try UNNotificationAttachment(identifier: "picture", url: urlPath, options: nil)
        completionHandler(attachment)
      } catch {
        completionHandler(nil)
      }
    }
    task.resume()
  }
构建应用程序时,它将使用此代码来加载通知

发送通知时,必须记住包含“图标”值。发送通知所需内容的示例:

    "notification": {
        "body": "body",
        "title": "title"
        "mutable_content": true // this row is required for the notification to work!
    },
    "data": {
        "icon":"https://pusher.com/static_logos/320x320.png", // change to your image url.
    },

这是否适用于本地通知(不是来自APN的通知)