Is it possible to prevent a remote notification fr

2020-02-13 08:11发布

问题:

I'd like to better control what notifications are being displayed to my users and selectively silence some of them. In order to do this I have implemented a UNNotificationServiceExtensionin my app, which allows me to intercept and modify notifications even when my app is not running. The problem however is that even if I don't call didReceive(_:withContentHandler:) the system will still display the remote notification after approximately 30 seconds. How can I prevent this from happening?

回答1:

As of iOS 11, it is not possible to suppress push notifications from being displayed using a UNNotificationServiceExtension.

In WWDC 17's Best Practices and What’s New in User Notifications, Teja states explicitly that such a thing cannot be done (starting at 22:17 min):

All work should be either about modifying or enhancing this notification. The service extension also doesn't have the power to drop this notification or prevent it from being displayed. This notification will get delivered to the device. If instead you want to launch your application in the background and run some additional processing, you should send a silent notification. You can also send a silent notification and launch your app in the background and your app can determine whether or not to schedule a local notification if you want to present a conditional notification.



回答2:

Just for completeness:

When the app is not active (in the background or killed) Landschaft's answer does apply: one cannot suppress any push notification.

But if the app is active in the foreground it is possible to suppress push notifications.

Instead of using the app extension, one needs to implement the willPresent function from the UNUserNotificationCenterDelegate.

Here one can filter the notifications and in the completionHandler return how it is allowed to be presented:
• display nothing: completionHandler([])
• display only alert: completionHandler([.alert])
• display alert with sound: completionHandler([.alert, .sound])
• etc...

We wanted to display local notifications but never display push notifications as we handle them in-app with a custom UI:

func userNotificationCenter(_ center: UNUserNotificationCenter, 
                            willPresent notification: UNNotification, 
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    if response.notification.request.trigger is UNPushNotificationTrigger {
        completionHandler([])
    } else {
        completionHandler([.alert, .badge])
    }
}