如何从通知服务扩展中删除已传递的通知?

3
我们正在使用通知服务扩展来显示两种类型的通知:呼叫和取消。前者显示消息“您正在被呼叫”。后者将显示“您已被呼叫”。
当涉及到后一种通知时,我们希望在仍然在通知中心中显示通知的情况下删除所有先前创建的呼叫通知。这必须在应用程序处于后台和终止状态下工作。
为此,我们可以使用UNUserNotificationCenter.current().removeDeliveredNotifications
然而,在通知服务扩展中,无论应用程序是否已经创建了仍然在通知中心中显示的通知getDeliveredNotificationsgetPendingNotificationRequests都会返回空列表。
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)")
}

有人知道为什么会出现这样的行为吗?有什么替代方法吗?

1个回答

0
你可以从中获取通知标识符
func didReceive(_ notification: UNNotification) {
     notificationIdentifier = notification.request.identifier
}

然后你可以通过这个方法移除通知。
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [notificationIdentifier])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接