UNUserNotificationCenter removeAllDeliveredNotifications在ios 11.2中不起作用

人气:773 发布:2022-09-21 标签: ios uilocalnotification unnotificationrequest

问题描述

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

I have an app with multiple local notifications. When I try to clear all the delivered notifications, I call this removeAllDeliveredNotifications method. It's working fine till ios 11.1. In ios 11.2 and above, it doesn't work as expected. The notification still remains in the notification center. Could someone please help me out on this.

提前致谢。

推荐答案

它仍在为我们工作。我刚刚在iOS 11.2.2上查看过它。 我在 removeDeliveredNotificationsWithIdentifiers:里面 getDeliveredNotificationsWithCompletionHandler:,调用 getDeliveredNotificationsWithCompletionHandler 。

It is still working for us. I just checked it on iOS 11.2.2. I am using removeDeliveredNotificationsWithIdentifiers: inside getDeliveredNotificationsWithCompletionHandler:, calling getDeliveredNotificationsWithCompletionHandler on Main Thread.

- (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时遇到奇怪的行为。如果暂停时间过长(无论这意味着什么),完成处理程序将无法完成(即使在继续进程执行时),导致应用程序无响应。也许完成处理程序会被终止。

Though I experience strange behavior if I am debugging the completionHandler. If a pause too long (whatever that means) the completion handler will not finish (even on continue process execution) resulting in an unresponsive app. Maybe the completionhandler gets terminated.

934