我们如何显示图像或任何带有本地通知的附件,就像我们以前在丰富通知中显示的那样?
我正在接收静默通知,然后在本地通知中对其进行更改。
答案 0 :(得分:3)
是的,您也可以在本地通知中显示它,您必须在收到静默推送后触发本地通知,我希望在平稳的通知有效负载中所有需要数据的地方都存在。
这是代码段
let content = UNMutableNotificationContent()
//Configure notification
content.title = "Notification Title"
content.body = "Notification Body"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "ImageNotification"
//Attach your image local path here (Document dir path)
let attachment = try! UNNotificationAttachment(identifier: "\(NSDate().timeIntervalSince1970 * 1000)", url: localURL, options: [:])
content.attachments = [attachment]
content.userInfo = ["attachmentType": "Media"]
// Create a trigger for fire a local notification
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.2, repeats: false)
let request = UNNotificationRequest(identifier: "\(NSDate().timeIntervalSince1970 * 1000)", content: content, trigger: trigger)
// Configure according to version
if #available(iOS 11.0, *) {
let contactCategory = UNNotificationCategory(identifier: content.categoryIdentifier,
actions: [],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "",
options: .customDismissAction)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.setNotificationCategories([contactCategory])
} else {
// Fallback on earlier versions
let contactCategory = UNNotificationCategory(identifier: content.categoryIdentifier, actions: [], intentIdentifiers: [], options: [])
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.setNotificationCategories([contactCategory])
}
UNUserNotificationCenter.current().add(request) {[weak self] (error) in
guard error == nil else {
return
}
}
此工具实施后可以正常工作,如果您仍然遇到任何问题,请告诉我。