我可以使用修改远程通知的内容"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 }
1 回答
我写了一个扩展来简化这个过程,请看这里:UNNotificationAttachment with UIImage or Remote URL
然后你可以像这样包含图像