【发布时间】:2022-01-17 06:37:58
【问题描述】:
我的应用使用了很多预定的本地通知,并且在某些事件中我重新安排了通知并希望清除一些已发送的通知,而不是全部。
粗略的伪代码:
// Clear pending notifications that haven't been delivered yet
notificationCenter.removeAllPendingNotificationRequests()
// Get the delivered notifications (async), filter out the ones that should be removed
// and remove them
notificationCenter.getDeliveredNotifications() { notifications in
let notificationsToRemove = notifications.filter { some boolean operation }
let identifiersToRemove = notificationsToRemove.map { $0.identifier }
notificationCenter.removeDeliveredNotificationsWithIdentifiers(identifiersToRemove)
}
// Schedule the next set of notifications
let nextBatchOfNotifications = notificationGenerator.generate()
for notification in nextBatchOfNotifications) {
notificationCenter.schedule(notification)
}
但是当我这样做时,绝大多数情况下都会清除所有已发送的通知。在极少数情况下,它只会导致我要求删除的部分已发送通知被删除(或者可能没有)。
【问题讨论】:
标签: ios usernotifications