Group Group Group Group Group Group Group Group Group

Removing delivered notifications


Let me just say that the book indeed offers a deep dive into push notifications and related matters. I really liked it. There would be one topic that I would have like to read for example and that I had to deal with in my code base.

In chapter 13 it is mentioned how one can remove delivered notifications and this is applied to local notifications. It would be nice to have a section in the book about how to remove push notifications. Of course you would use the same method as for Local notifications but the process is not always straight forward. For example:

Let’s suppose in an app users receive “new offer” notifications. If one of the users accepts the offer this notification should be removed from other users notification screens. One way to do it is sending a silent notification. This hits didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler. Here we then call a handler that would do any logic. For simplicity let’s call it RemoveOfferHandler.

The issue that we found here is that calling removeDeliveredNotifications is not that straight forward. We had to first call beginBackgroundTask, execute the remove function and then endBackgroundTask. Thats because removeDeliveredNotifications closure needs to run in the background.

So this is just to give an idea of what complication can arise and would be really nice if this would be discussed in the book.

@gargoyle Do you have any feedback about this? Thank you - much appreciated! :]

@gargoyle Any feedback would be appreciated especially if there is a better way to deal with this :slight_smile:

Why do you say that the removeDeliveredNotifications needs to run in the background? There shouldn’t be any such requirement. When you call that method iOS will perform the operation asynchronously, so the remove call will immediately return and then it’ll go off and do the removes.

@gargoyle Thanks for replying. To be able to execute getDeliveredNotifications and removeDelieveredNotifications methods when app is in the background state we need to tell OS to allow app to execute those operations asynchronously otherwise when the main queue finishes the OS suspends the app immediately. By calling beginBackgroundTask and endBackgroundTask, we tell OS to wait app until app runs getDelieveredNotifications and removeDelieveredNotifications. However, since we don’t know when removeDelieveredNotification finishes executing (since it is asynchronous) we just wait 2 seconds and then call endBackgroundTask. If there is a way to understand the moment when removeDelieveredNotification is finished We can call endBackgroundTask smartly rather than putting a fixed timeout because it is kind of a hack.

Sorry I just saw this message! Not sure why the forum didn’t mail me when you posted. Those methods don’t provide any type of callback to know when they’re done so unfortunately you can’t really do what you are asking.