丰富的本地通知
Posted
技术标签:
【中文标题】丰富的本地通知【英文标题】:Rich local notification 【发布时间】:2018-10-12 04:47:49 【问题描述】:我们如何像以前在富通知中显示的那样显示带有本地通知的图像或任何附件?
我正在接收静默通知,然后在本地通知中更改它。
【问题讨论】:
【参考方案1】:是的,您也可以在本地通知中显示它,您必须在收到静默推送后触发本地通知,我希望您的静默通知负载中所有需要的数据都在那里。
这里是代码sn-p
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
在这个实现之后它会正常工作,如果您仍然遇到任何问题,请告诉我。
【讨论】:
以上是关于丰富的本地通知的主要内容,如果未能解决你的问题,请参考以下文章