gpt4 book ai didi

ios - 使用相同的请求标识符同时注册 2 个本地通知而不覆盖

转载 作者:行者123 更新时间:2023-11-30 11:13:24 25 4
gpt4 key购买 nike

我正在创建一个带有本地通知的提醒,其中我必须使用某个标识符注册一个本地通知,比如 X,它应该在定义的时间间隔内触发而不重复,同时我想注册相同的通知10 分钟后,具有相同标识符的第一个被解雇。

就我而言,如果第一个通知被触发并且用户没有对其采取任何操作,那么 10 分钟后如果相同的通知被触发,那么在通知中心(托盘)中不应有 2 个通知。相反,应该只显示最后一条通知。

下面是我为实现此目的所做的代码:

    notificationContent.categoryIdentifier = Notification.Category.tutorial


let notificationTrigger1 = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)

let request1 = UNNotificationRequest(identifier: "FiveSecond", content: notificationContent, trigger: notificationTrigger1)

center.add(request1, withCompletionHandler: {(_ error: Error?) -> Void in
if error == nil {
print("add NotificationRequest1 succeeded!")
center.removePendingNotificationRequests(withIdentifiers: [Notification.Category.tutorial])
}
})


let notificationTrigger2 = UNTimeIntervalNotificationTrigger(timeInterval: 60*10, repeats: true)

let request2 = UNNotificationRequest(identifier: "FiveSecond", content: notificationContent, trigger: notificationTrigger2)

center.add(request2, withCompletionHandler: {(_ error: Error?) -> Void in
if error == nil {
print("add NotificationRequest2 succeeded!")
center.removePendingNotificationRequests(withIdentifiers: [Notification.Category.tutorial])
}
})

在此代码中,问题是第一个通知未触发,仅在 10 分钟后触发第二个通知。

最佳答案

您将两次删除先前安排的通知:

if error == nil {
print("add NotificationRequest2 succeeded!")
//remove previous notifications
center.removePendingNotificationRequests(withIdentifiers: [Notification.Category.tutorial])
}

您应该在应用程序空闲时提前设置这两个通知,假设用户不会使用react - 这是您设置这两个通知的唯一机会。

要在用户执行操作时删除第二个通知,就是删除处理该操作的代码中不需要的通知,或者在应用程序激活时处理该逻辑。

关于ios - 使用相同的请求标识符同时注册 2 个本地通知而不覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51966881/

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