UNUserNotificationCenter removeAllDeliveredNotifications not working in ios 11.2
Asked Answered
R

3

11

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.

Thanks in advance.

Reld answered 5/1, 2018 at 6:13 Comment(11)
I had code that cleaned up notifications that stopped working in 11.2. Where are you calling it? I've found it works when I call it in a notification action, but doesn't work when called in the app, from a Today widget, or in the background after a message is received from watchOS. All worked fine before. Seems like a bug in iOS to me. Requested a TSI from Apple, but I'm not able to replicate the bug in a new project from scratch even though it uses the same approach as my app.Haw
Also, when I check the console after calling it I'm seeing SpringBoard(UserNotificationsServer)[62] <Error>: Could not load data at /var/mobile/Library/SpringBoard/PushStore/APP_BUNDLE_STRING.pushstore followed by SpringBoard(UserNotificationsServer)[62] <Notice>: Saving notification list at /var/mobile/Library/SpringBoard/PushStore/APP_BUNDLE_STRING.pushstore with 0 items (where APP_BUNDLE_STRING is my app's bundle string) Are you seeing the same? Have you tried a TSI or have you filed a radar?Haw
Thank you for your reply. I had called 'removeAllDeliveredNotifications' inside the app. As you said, the issue exist in iOS 11.2 and above. I have filed the radar.Reld
Same thing happening to me, did you find any solution?Olid
@Olid At this point it seems like an iOS bug to me, but I haven't been able to replicate it in a fresh project. Are you able to reproduce the bug in a new project?Haw
@Haw yes, I tried in a fresh project, which basically sent a local notification inside the app. getDeliveredNotificationsWithCompletionHandler returned empty array.Olid
@Haw I too tried in a new project. This issue exist. getDeliveredNotificationsWithCompletionHandler returns an empty array.Reld
Someone filed this open radarRoundly
This is my radar, feel free to copy and file to Apple, and hopefully they will fix this soon.Roundly
I also came across this radar for the original problem described on this question where delivered notifications can't be cleared.Haw
It looks like this issue has been resolved in iOS 10.3 based on my initial testing.Haw
P
2

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];
    }];
}

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.

Pyretic answered 15/1, 2018 at 13:10 Comment(12)
Thanks for the extra info. I'm going nuts trying to figure out what's happening. I was originally doing something similar to what you're doing to only clear specific notifications, but during troubleshooting I've been using UNUserNotificationCenter.current().removeAllDeliveredNotifications(), which should always be clearing all of them, but it's not. I'm also not even calling any other code when I want to clear the notifications to try to give it enough time to run, but it still only works in the one situation (when called from a notification action in a UNUserNotificationCenterDelegate).Haw
@Haw Can you describe more how it works in UNUserNotificationCenterDelegate? I am guessing this delegate method is called when you are acting on the notification, where the current bug is that all notifications will be cleared once any system or custom action is applied to one of the notifications.Roundly
@Pyretic Which device or simulator are you running this?Roundly
@HarryNg iPhone XPyretic
Thanks for reply. So I tried on iPhone X with 11.2.2, the logic does not work. I was building in both Xcode 9.1 and 9.2 using swiftRoundly
@HarryNg what details do you want to know about my implementation on UNUserNotificationCenterDelegate? I basically have some custom actions including a custom dismiss action. They all call removeAllDeliveredNotifications() and work as expected when I take action or dismiss a notification. But when I call removeAllDeliveredNotifications() any other time, like when a button in my app is tapped, for example, the notifications do not get removed and I get that console output I mentioned before.Haw
@Haw Thanks for reply. There are two things, if you don’t call removeAll in delegate, will they still remove? Secondly, if you call remove by identifier instead, does that work?Roundly
@HarryNg If I don’t call it in the notification action other notifications do not get removed (the one the action is taken on gets removed, which is normal behavior, but the others remain in the Notification Center). Yes, calling remove with identifiers also works when called in response to a notification action, but not when called other places, like when tapping a button in my app.Haw
@HarryNg after further testing I’ve found all notifications are being cleared with a notification action even when I don’t call removeAllDeliveredNotifications()Haw
@Haw makes sense then, the bug is reproducible on iOS 11.2 onwardsRoundly
apology for this stupid question: are you calling getDeliveredNotificationsWithCompletionHandler? as getPendingNotificationRequestsWithCompletionHandler would not return the desired notificationsPyretic
@Pyretic I'm using getDeliveredNotifications(completionHandler:) to identify the delivered notifications and remove them from the Notification Center with removeDeliveredNotifications(withIdentifiers:), but neither that nor removeAllDeliveredNotifications() is removing them. There also seems to be a separate (but possibly related) bug that @HarryNg pointed out, where performing any notification action clears all delivered notifications when removeAllDeliveredNotifications() is not even called.Haw
H
-1

This appears to be a bug in iOS that was fixed in iOS 11.3.

Haw answered 30/3, 2018 at 20:12 Comment(3)
Did you see that the problem happens in iOS 11.2 and above?Drawl
@TomCalmon I have not encountered it again after it was fixed in iOS 11.3Haw
Right! Your answer was talking about iOS 10.3. I saw that you did correct for iOS 11.3!Drawl
F
-2

Did you try using this method:

removeDeliveredNotifications(withIdentifiers:)

You will need to pass an array of all the notification identifiers that you need to delete.

Fabianfabianism answered 10/1, 2018 at 11:56 Comment(1)
I'm having the same issue, but using the identifiers does not solve it all the time. removeAllDeliveredNotifications and removeDeliveredNotifications(withIdentifiers:) seem to work in some situations, but not consistently, since iOS 11.2.Haw

© 2022 - 2024 — McMap. All rights reserved.