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

6

16

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?

Seychelles answered 19/10, 2016 at 3:53 Comment(7)
In the notification docs (developer.apple.com/library/content/documentation/…) you can see that it says To support silent remote notifications, add the remote-notification value to the UIBackgroundModes array in your Info.plist file.Smallclothes
@Smallclothes But I do want some notifications to show. I want the decision to be up to the client and not the server. Making some silent notifications show is not possible to my knowledge, so I'm trying to see if i can silent some regular notifications.Seychelles
Ah, in that case I'm not too sure. iOS 10 notifications changes are something on my list to look at, sorry!Smallclothes
Just a thought (haven't tried this personally yet), have you tried sending a silent notification from your server and then having the app create a local notification, if needed?Instigate
That's the right answer JRG-DeveloperThingumajig
@Instigate I assume that this will not start the app if it is not running in fore- or background. The reason to use an extension is just that smart thing you could access notifications even the app was not started.Binding
Can confirm that creating a local notification via silent notification is not reliable - if the user has force quit the app then nothing will happen.Intercessory
S
3

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?

Schaffel answered 28/10, 2016 at 5:1 Comment(0)
F
1

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

Focalize answered 26/4, 2021 at 15:43 Comment(0)
T
0

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

Than answered 28/7, 2017 at 10:23 Comment(0)
E
0

@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

Edraedrea answered 16/5, 2019 at 3:54 Comment(0)
A
-1

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.

Aphesis answered 21/10, 2016 at 16:44 Comment(1)
That used to be case prior to iOS 10, but with iOS 10 you can certainly take actions after it's sent to APNS. You can even change the content with UNNotificationServiceExtension. So the question is, if you can change the content, why can you just cancel it? changing the content to nil didn't help.Seychelles
O
-1

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.

Outgoings answered 21/10, 2016 at 21:37 Comment(1)
This wont work. "Notification service app extensions only operate on remote notifications that are configured to display an alert to the user. If alerts are disabled for your app, or if the payload specifies only the playing of a sound or the badging of an icon, the extension is not employed." developer.apple.com/documentation/usernotifications/…Tenotomy

© 2022 - 2024 — McMap. All rights reserved.