【发布时间】:2016-12-28 02:01:52
【问题描述】:
我一直致力于开发 iOS10 中引入的丰富通知体验,并坚持将图像作为附件传递给 UNNotificationContentExtension。
这是我的 ContentExtension:
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet weak var attachmentImage: UIImageView!
func didReceive(_ notification: UNNotification) {
if let attachment = notification.request.content.attachments.first {
if attachment.url.startAccessingSecurityScopedResource() {
attachmentImage.image = UIImage(contentsOfFile: attachment.url.path)
attachment.url.stopAccessingSecurityScopedResource()
}
}
}
}
作为教程,我一直关注Advanced Notifications video from WWDC。
我已经检查过 - UIImage 我正在分配给 UIImageView:
- 是不是
nil - 有正确的
CGSize(191x191) - attachment.url.path 等于
/var/mobile/Library/SpringBoard/PushStore/Attachments/<bundle of app>/<...>.png
以下是我从应用发送本地通知的方式:
let content = UNMutableNotificationContent()
content.title = "Sample title"
content.body = "Sample body"
content.categoryIdentifier = "myNotificationCategory"
let attachement = try! UNNotificationAttachment(identifier: "image",
url: Bundle.main.url(forResource: "cat", withExtension: "png")!,
options: nil)
content.attachments = [ attachement ]
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: nil)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
}
}
"cat.png" 只是我添加到 proj 的一个虚拟资源。
如您所见,通知显示图片,所以我假设我发送正确,但在展开状态(NotificationViewController)我从未成功显示相同的图像。
我做错了什么? 谢谢!
【问题讨论】:
-
您好,我仍然面临无法在展开状态下显示图像的问题。你能帮帮我吗?我的代码在目标 c 中。
标签: ios swift ios10 unnotificationattachment