How can I cancel or stop push notification from a

2019-03-24 16:17发布

问题:

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?

回答1:

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?



回答2:

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.



回答3:

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.



回答4:

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