我们正在使用通知服务扩展来显示两种类型的通知:呼叫和取消。前者显示消息“您正在被呼叫”。后者将显示“您已被呼叫”。
当涉及到后一种通知时,我们希望在仍然在通知中心中显示通知的情况下删除所有先前创建的呼叫通知。这必须在应用程序处于后台和终止状态下工作。
为此,我们可以使用
然而,在通知服务扩展中,无论应用程序是否已经创建了仍然在通知中心中显示的通知
当涉及到后一种通知时,我们希望在仍然在通知中心中显示通知的情况下删除所有先前创建的呼叫通知。这必须在应用程序处于后台和终止状态下工作。
为此,我们可以使用
UNUserNotificationCenter.current().removeDeliveredNotifications
。然而,在通知服务扩展中,无论应用程序是否已经创建了仍然在通知中心中显示的通知
getDeliveredNotifications
和getPendingNotificationRequests
都会返回空列表。UNUserNotificationCenter.current().getPendingNotificationRequests { notifications in
print("[NotificationService] Current pending notifications requests count: \(notifications.count)")
}
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
print("[NotificationService] Current notifications count: \(notifications.count)")
}
有人知道为什么会出现这样的行为吗?有什么替代方法吗?