我正在尝试自定义通知的默认设计,文本设置工作正常。但是,当我尝试添加附件时,通知不会显示附件。下面是我的didRecive方法。根据教程,他们只是这样做。我可以使用
获取图像的网址let url = Bundle.main.url(forResource: "download", withExtension: "png")
但是当我添加附件时,整个通知设置为默认。
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
print("Notification Service")
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
let data : [String : Any] = request.content.userInfo as! [String : Any]
let badgeCount : Int = data["badgeCount"] as? Int ?? 0
let notification : [String : Any]? = data["notification"] as? [String : Any]
bestAttemptContent.badge = NSNumber(integerLiteral: badgeCount)
if (notification != nil) {
bestAttemptContent.title = notification!["title"] as! String
bestAttemptContent.body = notification!["body"] as! String
}
let url = Bundle.main.url(forResource: "download", withExtension: "png")
let attach = try! UNNotificationAttachment(identifier: "xyzzz", url: url!, options: nil)
bestAttemptContent.attachments = [attach]
contentHandler(bestAttemptContent)
}
}
图片尺寸为:900 * 900