带图像的PHP-iOS推送通知


trpnest
2025-03-10 11:36:50 (10天前)
  1. 我正在尝试将带有图像内容的推送通知从我的Web应用程序发送到iOS App。我收到了通知,其中包含了我提供的所有文字和正文信息。但是给定的图像是......

2 条回复
  1. 0# 谦逊的毛巾 | 2019-08-31 10-32



    要显示图像,音频和视频等媒体内容,您需要在iOS应用中添加NotificationServiceExtension。对于要执行的iOS应用程序中的NotificationServiceExtension,您需要将可变内容值发送为1,这在您提到的有效负载中看起来很好。
    在NotificationServiceExtension中,您将在大约10秒内从通知有效负载中发送的URL下载映像。下载图像后,您需要将图像保存在FileManager中。之后,使用文件图像URL初始化UNNotificationAttachment并将其传递给完成处理程序。 PFA代码如下




    1. import UserNotifications

    2. </code>


    class NotificationService:UNNotificationServiceExtension {




    1. var contentHandler: ((UNNotificationContent) -> Void)?
      var bestAttemptContent: UNMutableNotificationContent?

    2. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
      self.contentHandler = contentHandler
      bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
      if let bestAttemptContent = bestAttemptContent {
      // Modify the notification content here…
      var urlString:String? = nil
      if let urlImageString = request.content.userInfo[“urlImageString”] as? String {
      urlString = urlImageString
      }

    3.     if urlString != nil, let fileUrl = URL(string: urlString!) {
    4.         print("fileUrl: \(fileUrl)")
    5.         guard let imageData = NSData(contentsOf: fileUrl) else {
    6.             contentHandler(bestAttemptContent)
    7.             return
    8.         }
    9.         guard let attachment = UNNotificationAttachment.saveImageToDisk(fileIdentifier: "image.jpg", data: imageData, options: nil) else {
    10.             print("error in UNNotificationAttachment.saveImageToDisk()")
    11.             contentHandler(bestAttemptContent)
    12.             return
    13.         }
    14.         bestAttemptContent.attachments = [ attachment ]
    15.     }
    16.     contentHandler(bestAttemptContent)
    17. }
    18. }

    19. override func 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.
      if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
      contentHandler(bestAttemptContent)
      }
      }

    20. </code>


    }



    @available(iOSApplicationExtension 10.0,*)
    扩展名UNNotificationAttachment {




    1. static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
      let fileManager = FileManager.default
      let folderName = ProcessInfo.processInfo.globallyUniqueString
      let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true)

    2. do {
    3.     try fileManager.createDirectory(at: folderURL!, withIntermediateDirectories: true, attributes: nil)
    4.     let fileURL = folderURL?.appendingPathComponent(fileIdentifier)
    5.     try data.write(to: fileURL!, options: [])
    6.     let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL!, options: options)
    7.     return attachment
    8. } catch let error {
    9.     print("error \(error)")
    10. }
    11. return nil
    12. }

    13. </code>


    }


登录 后才能参与评论