【发布时间】:2021-08-13 13:56:18
【问题描述】:
我正在尝试编写一个函数,该函数发送包含私人用户数据的通知,可以使用以下代码在 5 秒后自动删除:
Button("Send Notification that self deletes after 5 seconds") {
let content = UNMutableNotificationContent()
content.title = "Notification Title"
content.body = "Body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
DispatchQueue.main.asyncAfter(deadline: .now() + 10, execute: {
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
})
但是,通知仍然保留在通知中心和锁定屏幕上。我只能通过打开应用程序让UNUserNotificationCenter.current().removeAllDeliveredNotifications() 工作。我已经为这个答案搜索了几个小时的解决方案,但似乎找不到。非常感谢您的帮助。
【问题讨论】:
标签: ios swift unusernotificationcenter