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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sheik_101
  • 770
  • 1
  • 8
  • 24
  • 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. – gohnjanotis Jan 10 '18 at 16:32
  • Also, when I check the console after calling it I'm seeing `SpringBoard(UserNotificationsServer)[62] : Could not load data at /var/mobile/Library/SpringBoard/PushStore/APP_BUNDLE_STRING.pushstore` followed by `SpringBoard(UserNotificationsServer)[62] : 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? – gohnjanotis Jan 10 '18 at 16:47
  • 2
    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. – Sheik_101 Jan 11 '18 at 05:41
  • Same thing happening to me, did you find any solution? – Saleh Jan 15 '18 at 16:34
  • @Saleh 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? – gohnjanotis Jan 15 '18 at 16:49
  • 1
    @gohnjanotis yes, I tried in a fresh project, which basically sent a local notification inside the app. `getDeliveredNotificationsWithCompletionHandler` returned empty array. – Saleh Jan 15 '18 at 16:58
  • @gohnjanotis I too tried in a new project. This issue exist. getDeliveredNotificationsWithCompletionHandler returns an empty array. – Sheik_101 Jan 16 '18 at 07:10
  • 1
    Someone filed this [open radar](https://openradar.appspot.com/36019616) – Harry Ng Jan 17 '18 at 08:28
  • 1
    This is my [radar](https://openradar.appspot.com/36575570), feel free to copy and file to [Apple](https://bugreport.apple.com/web/), and hopefully they will fix this soon. – Harry Ng Jan 17 '18 at 08:42
  • I also came across [this radar](https://openradar.appspot.com/35676570) for the original problem described on this question where delivered notifications can't be cleared. – gohnjanotis Jan 21 '18 at 20:20
  • It looks like this issue has been resolved in iOS 10.3 based on my initial testing. – gohnjanotis Mar 30 '18 at 19:43

3 Answers3

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.

cornr
  • 653
  • 4
  • 20
  • 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). – gohnjanotis Jan 15 '18 at 16:14
  • @gohnjanotis 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. – Harry Ng Jan 17 '18 at 08:11
  • @cornr Which device or simulator are you running this? – Harry Ng Jan 17 '18 at 08:13
  • @HarryNg iPhone X – cornr Jan 17 '18 at 09:19
  • 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 swift – Harry Ng Jan 17 '18 at 10:01
  • @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. – gohnjanotis Jan 17 '18 at 14:56
  • @gohnjanotis 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? – Harry Ng Jan 17 '18 at 15:02
  • @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. – gohnjanotis Jan 17 '18 at 15:06
  • @HarryNg after further testing I’ve found all notifications are being cleared with a notification action even when I don’t call `removeAllDeliveredNotifications()` – gohnjanotis Jan 18 '18 at 01:31
  • @gohnjanotis makes sense then, the bug is reproducible on iOS 11.2 onwards – Harry Ng Jan 18 '18 at 01:34
  • apology for this stupid question: are you calling `getDeliveredNotificationsWithCompletionHandler`? as `getPendingNotificationRequestsWithCompletionHandler` would not return the desired notifications – cornr Jan 18 '18 at 16:36
  • @cornr 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. – gohnjanotis Jan 18 '18 at 18:19
-1

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

gohnjanotis
  • 6,513
  • 6
  • 37
  • 57
-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.

atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • 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. – gohnjanotis Jan 10 '18 at 15:21