我可以使用通知服务扩展的“func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)”来修改远程通知的内容。但无法下载图像或电影并将它们作为附件添加到内容中。
我们如何使用这种方法在远程通知中附加媒体。
我可以使用通知服务扩展的“func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)”来修改远程通知的内容。但无法下载图像或电影并将它们作为附件添加到内容中。
我们如何使用这种方法在远程通知中附加媒体。
我写了一个扩展来简化这个过程,见这里: UNNotificationAttachment with UIImage or Remote URL
然后你可以像这样包含图像
let identifier = ProcessInfo.processInfo.globallyUniqueString
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "World"
if let attachment = UNNotificationAttachment.create(identifier: identifier, image: myImage, options: nil) {
// where myImage is any UIImage that follows the
content.attachments = [attachment]
}
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120.0, repeats: false)
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
// handle error
}