Andrey Chernukha
Andrey Chernukha

Reputation: 21808

Is it possible to detect the moment when a remote notification has been added to the notification center?

There are two methods in UNUserNotificationCenterDelegate protocol related to the notification delivery. But both don't seem to satisfy my needs.

-(void)userNotificationCenter:(UNUserNotificationCenter *)center
  willPresentNotification:(UNNotification *)notification
    withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler

is called BEFORE the notification has been delivered. If you ask the notification center to give you all the delivered notifications the new one will be missing.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
     withCompletionHandler:(void(^)(void))completionHandler

is called only if the user provides some response to the notification. There is also good old - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo but it is deprecated.

So is it even possible to be notified right after the Notification Center DID add a notification?

Upvotes: 0

Views: 314

Answers (2)

Cristik
Cristik

Reputation: 32804

You can add a Notification Content Extension, this extension will be called when push notifications are received, with the goal of allowing the extension to enhance the notification before presenting it to the user (wwdc session on advanced notifications).

This way you have a chance to execute some code when notification arrive. You don't have to actually modify the notification.

Note that the code will execute in the context of the app extension, so you might need some more work if you want to execute something within the app.

Upvotes: 2

Glenn Posadas
Glenn Posadas

Reputation: 13281

What you want is this:

delete the notification from the notification center. but it is not there

If you're trying to suppress or prevent the push notification from presenting. Then this is not possible, as of iOS 11.0 As to why, Apple didn't mention.

However, if you want to just remove a push, UNUserNotificationCenter has the following methods available for you to use:

// Notification requests that are waiting for their trigger to fire
open func getPendingNotificationRequests(completionHandler: @escaping ([UNNotificationRequest]) -> Void)

open func removePendingNotificationRequests(withIdentifiers identifiers: [String])

open func removeAllPendingNotificationRequests()


// Notifications that have been delivered and remain in Notification Center. Notifications triggered by location cannot be retrieved, but can be removed.
open func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)

open func removeDeliveredNotifications(withIdentifiers identifiers: [String])

open func removeAllDeliveredNotifications()

Upvotes: 1

Related Questions