gpt4 book ai didi

ios - 丰富的本地通知

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:12 25 4
gpt4 key购买 nike

我们如何才能像过去在丰富的通知中显示的那样在本地通知中显示图像或任何附件?

我正在接收静默通知,然后在本地通知中更改它。

最佳答案

是的,你也可以在本地通知中显示它,你必须在收到静默推送后触发本地通知,我希望你的静默通知负载中所有需要的数据都在那里。

这是代码片段

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
}
}

此实现后它会正常工作,如果您仍然遇到任何问题,请告诉我。

关于ios - 丰富的本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52772446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com