我一直致力于开发 iOS10 中引入的丰富通知体验,并坚持将图像作为附件传递到UNNotificationContentExtension
.
这是我的内容扩展:
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()
}
}
}
}
作为教程,我一直在关注WWDC 的 Advanced Notifications 视频。我已经检查过 -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){
}
}
如您所见,通知显示图片,所以我假设我发送正确,但在展开状态(in NotificationViewController
)我从未成功显示相同的图像。
我究竟做错了什么?谢谢!