jjzjj

ios - UNUserNotificationCenter removeAllDeliveredNotifications 在 ios 11.2 中不起作用

coder 2023-07-29 原文

我有一个包含多个本地通知的应用程序。当我尝试清除所有已发送的通知时,我调用此 removeAllDeliveredNotifications 方法。它工作正常,直到 ios 11.1。在 ios 11.2 及更高版本中,它无法按预期工作。通知仍然保留在通知中心。有人可以帮我解决这个问题吗?

提前致谢。

最佳答案

它仍在为我们工作。我刚刚在 iOS 11.2.2 上检查过它。 我在 getDeliveredNotificationsWithCompletionHandler: 中使用 removeDeliveredNotificationsWithIdentifiers:,在主线程上调用 getDeliveredNotificationsWithCompletionHandler

- (void)removePendingNotificationsForObjectID:(SJFObjectID *)objectID {
    __weak __typeof(self) weakSelf = self;
    [self.userNotificationCenter getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *notifications) {
        __strong __typeof(weakSelf) self = weakSelf;
        NSMutableArray <NSString *> *identifiersToRemove = [@[] mutableCopy];
        for (UNNotification *notification in notifications) {
            SJFObjectID *objectIDFromNotification = [self.notificationToObjectIDMarshaller marshalNotification:notification];
            if ([objectID isEqual:objectIDFromNotification]) {
                [identifiersToRemove addObject:notification.request.identifier];
            }
        }
        [self.userNotificationCenter removeDeliveredNotificationsWithIdentifiers:identifiersToRemove];
    }];
}

虽然我在调试 completionHandler 时遇到了奇怪的行为。如果暂停时间过长(无论这意味着什么),完成处理程序将无法完成(即使继续执行流程),从而导致应用程序无响应。也许完成处理程序被终止了。

关于ios - UNUserNotificationCenter removeAllDeliveredNotifications 在 ios 11.2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48108165/

有关ios - UNUserNotificationCenter removeAllDeliveredNotifications 在 ios 11.2 中不起作用的更多相关文章

随机推荐