我正在努力在 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"
}