【问题标题】:Media Attachment in iOS 10 Push NotificationsiOS 10 推送通知中的媒体附件
【发布时间】:2017-01-16 20:54:53
【问题描述】:

我正在努力在 iOS 10 中向我的推送通知中添加图像。

我添加了一个通知服务扩展,并使用了以下代码:

        override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)


    if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) {
        URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
            if let location = location {
                let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG]
                if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
                    self.bestAttemptContent?.attachments = [attachment]
                }
            }
            self.contentHandler!(self.bestAttemptContent!)
            }.resume()
    }
}

我从下面的第一个答案中得到了这个代码。

我现在遇到的问题是收到通知,有短暂的延迟,表明必须进行下载,但没有显示附件。

我假设正在调用 serviceExtensionTimeWillExpire() 并且只显示 bestAttempt

非常感谢任何帮助。

我相信我的 APNs 负载配置正确:

apns: {
  aps: { 
    alert: { 
      title: "Title", 
      subtitle: "Subtitle", 
      body: "Body"
    }, 
    "mutable-content": 1
  },
  "image-url": "https://helloworld.com/image.png" 
}

【问题讨论】:

    标签: ios apple-push-notifications ios10 ios-app-extension unnotificationserviceextension


    【解决方案1】:

    您必须从通知的用户信息中提取 url,然后下载图像并为附件提供文件 url。尝试类似:

    if let urlString = request.content.userInfo["image-url"] as? String, let fileUrl = URL(string: urlString) {
        URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
            if let location = location {
                let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG]
                if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
                    self.bestAttemptContent.attachments = [attachment]
                }
            }
    
            self.contentHandler(self.bestAttemptContent)
        }.resume()
    }  
    

    【讨论】:

    • 我似乎无法将更新后的代码用于电影或 GIF,只能使用图像。有什么建议吗?
    【解决方案2】:

    我已经设法使用以下代码解决了这个问题:

    斯威夫特:

    if let PusherNotificationData = request.content.userInfo["data"] as? NSDictionary {
                if let urlString = PusherNotificationData["image-url"] as? String, let fileUrl = URL(string: urlString) {
                    URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
                        if let location = location {
                            let options = [UNNotificationAttachmentOptionsTypeHintKey: kUTTypePNG]
                            if let attachment = try? UNNotificationAttachment(identifier: "", url: location, options: options) {
                                self.bestAttemptContent?.attachments = [attachment]
                            }
                        }
    
                        self.contentHandler!(self.bestAttemptContent!)
                        }.resume()
                }
            }
    

    节点:

    apns: {
        aps: { 
          alert: { 
            title: "title", 
            subtitle: "subtitle", 
            body: "body"
            }, 
            "mutable-content": 1,
            category: "test"
          },
        data: {
          "image-url": "www.helloworld.com/image.png"
        } 
      }
    

    感谢您的帮助!

    【讨论】:

    • 我似乎无法通过更改文件类型提示来添加 Gif 或视频。此外,删除此选项会阻止我的图像被加载。它仍然应该从 url 推断文件类型,对吧?
    【解决方案3】:

    另一种解决方案(也是可测试的解决方案)可能是将图像写入临时位置:

        // NotificationRequestHandler
        func getImageURL(from userInfo: [AnyHashable: Any]) throws -> URL {
            // parse the image URL and return it, otherwise throws and error
        }
    
        func temporaryWriteData(from url: URL) throws -> (String, URL) {
            let temporaryDirectoryUrl = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
            let temporaryFileName = UUID().uuidString
            let temporaryFileUrl = temporaryDirectoryUrl.appendingPathComponent(temporaryFileName)
    
            let data = try Data(contentsOf: url)
            try data.write(to: temporaryFileUrl, options: .atomic)
            return (temporaryFileName, temporaryFileUrl)
        }
    

    didReceive(_:withContentHandler:):

        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
    
        defer {
            contentHandler(bestAttemptContent ?? request.content)
        }
    
        do {
            let handler = NotificationRequestHandler()
            let imageUrl = try handler.getImageURL(from: request.content.userInfo)
            let (imageFileName, imageFileUrl) = try handler.temporaryWriteData(from: imageUrl)
            let attachment = try UNNotificationAttachment(identifier: imageFileName, url: imageFileUrl, options: [UNNotificationAttachmentOptionsTypeHintKey: "public.png"])
            bestAttemptContent?.attachments = [attachment]
        } catch {}
    

    debug extensions 也很有帮助 和how to test extentions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 2019-08-10
      • 2017-02-10
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多