user3586553
user3586553

Reputation:

How can I cancel or stop push notification from a client?

I have a webserver that sent a client a push notification. The client can intercept the push notification with UNNotificationServiceExtension on iOS 10 and change the content. Now I want to cancel a push notification from showing on client side on certain notifications. How do I do that? I tried to do

self.contentHandler(nil);

but it didn't work. How to do it?

Upvotes: 16

Views: 5841

Answers (6)

TontonRaoul
TontonRaoul

Reputation: 41

It's now possible since iOS 13.3. You need to pass an empty UNNotificationContent to contentHandler (not nil) and add the new filtering entitlement.

    // Determine whether you should suppress the notification.
    let suppress = myShouldSuppressNotification(request: request)
    
    if suppress {
        // Don't deliver the notification to the user.
        contentHandler(UNNotificationContent())        
    } else { ///...

Reference : https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_usernotifications_filtering

Upvotes: 1

Praveen Kumar
Praveen Kumar

Reputation: 1340

Pre iOS 10: I dont think it is possible. Here are the reasons. Once when the server sends a push notification to the client to Apple APNS server to send it to the clients, you lose the control of the push notifications. This is one of the reasons apple doesn't always promise 100 percent delivery of the notifications. This implicitly tells you that the Push notifications on the iPhone is handled by the iPhone operating system rather than your App.

Even the push notifications displayed in the Apple iPhone Springboard/dashboard is not in your control.

Users can either control whether he wants to receive the notifications or not.

So i don't think you can control the notifications partly after it is sent from your server.

Upvotes: -1

Porco
Porco

Reputation: 44

@Jorge Arimany's answer is to send the silent notification to wake up devices to do something, I think it's not the solution this user wants. And I think we still will get the general notification if we do nothing in our UNNotificationServiceExtension class

Upvotes: 0

Jorge Arimany
Jorge Arimany

Reputation: 5882

On iOS 10 and above you can send you a "silent" notification:

Apple doc: configure silent notification

Then you can decide if the notification should be shown to the user, if so, just create a user notification and show it:

Apple doc: local and push notifications

Upvotes: 0

Jota
Jota

Reputation: 149

It look like it might not be possible, but not sure. From the docs:

You can modify any of the content from the original request. You might customize the content for the current user or replace it altogether. You can use this method to download images or movies and add them as attachments to the content. You may also modify the alert text as long as you do not remove it. If the content object does not contain any alert text, the system ignores your modifications and delivers the original notification content.

https://developer.apple.com/reference/usernotifications/unnotificationserviceextension/1648229-didreceivenotificationrequest?language=objc

Calling the block with nil just displays the original notification?

Upvotes: 3

Josh Homann
Josh Homann

Reputation: 16327

I think the easiest way would be to send every push notification as a silent notification by default and only show the ones that you want to show. When you get a silent notification in

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])

You check your notification and determine if it should be displayed to the user. If not, you do nothing with it. If it needs to be shown then create a local notification and display it UILocalNotification (iOS 9 and earlier) or UNNotification (iOS 10).

I should add this has the disadvantage that no notifications will show if the app is not resident in memory (because the app needs to make the silent notification into a local notification), but if you were to take the opposite tact then you have the problem that something might be shown that shouldn't be show.

Upvotes: -1

Related Questions