0
我通过丰富的通知来显示通知中的图像,但每当我发送简单的消息,然后我收到通知。最后2天,我试图在通知中显示图片,但未完成。请帮助我做到这一点。swift丰富的通知。图像没有在通知中显示
预先感谢您
这是我的代码。
在通知服务扩展
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// Get the custom data from the notification payload
if let data = request.content.userInfo["data"] as? [String: String] {
// Grab the attachment
if let urlString = data["attachment-url"], let fileUrl = URL(string: urlString) {
// Download the attachment
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
// Move temporary file to remove .tmp extension
let tmpDirectory = NSTemporaryDirectory()
let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
let tmpUrl = URL(string: tmpFile)!
try! FileManager.default.moveItem(at: location, to: tmpUrl)
// Add the attachment to the notification content
if let attachment = try? UNNotificationAttachment(identifier: "", url: tmpUrl) {
self.bestAttemptContent?.attachments = [attachment]
}
}
// Serve the notification content
self.contentHandler!(self.bestAttemptContent!)
}.resume()
}
}
}
这是我的通知结构
{
"aps" : {
"alert" : {
"title" : "Push Remote Rich Notifications",
"subtitle" : "iOS 10 - New API",
"body" : "Media Image Rich notification"
},
"mutable-content" : 1,
"category" : "imageIdentifier"
},
"data" : {
"attachment-url": "https://raw.githubusercontent.com/Sweefties/iOS10-NewAPI-UserNotifications-Example/master/source/iOS10-NewAPI-UserNotifications-Example.jpg"
}
}
从DOC:“您的延伸部具有的时间(不超过30秒)来修改内容并执行contentHandler时块一个有限的量。如果您没有及时执行该模块,系统会调用您的扩展的serviceExtensionTimeWillExpire方法,为您提供最后一次执行该模块的机会。如果你不这样做,系统会向用户显示通知的原始内容。“你是否陷入这种情况? – Larme
我认为不,我不能管理....但我立即收到通知,但图像不显示 – Mohit
有你找到你的解决方案,我陷入了同样的境地 – riddhi