【发布时间】:2018-02-23 01:51:53
【问题描述】:
我制作了丰富的通知以在通知中显示图像,但每当我发送简单消息时,我都会收到通知。前 2 天我试图在通知中显示图像,但没有完成。请帮我做这件事。
提前致谢
这是我的代码。
在通知服务扩展中
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"
}
}
【问题讨论】:
-
来自文档:“您的扩展程序有有限的时间(不超过 30 秒)来修改内容并执行 contentHandler 块。如果您不及时执行该块, "你属于那种情况吗?
-
我认为不,我无法做到这一点....但我会立即收到通知,但图像不显示。
-
你找到解决方案了吗?我也陷入了同样的境地。
-
尝试可以在几秒钟内下载的非常小的图像。您使用的图片尺寸非常大。
-
你好@KrishnaDattShukla。使用 7 kb 大小的图像但仍无法获取图像
标签: ios swift3 notifications apple-push-notifications rich-notifications