Is it possible to prevent a remote notification from being displayed?

后端 未结 4 1754
旧巷少年郎
旧巷少年郎 2020-12-10 04:06

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 UNNotificationS

相关标签:
4条回答
  • 2020-12-10 04:32

    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.

    0 讨论(0)
  • 2020-12-10 04:42

    It's really easy to miss a comment above by @Lepidopteron and falsely assume there is absolutely no way to suppress the push notification - there is, and it's called silent push notification. It has a few limitations though. As stated in Apple's docs, you can send only 2-3 of these an hour, and there is no delivery guarantee.

    0 讨论(0)
  • 2020-12-10 04:50

    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])
        }
    }
    
    0 讨论(0)
  • 2020-12-10 04:52

    swift 5 iOS 11.x

    I got this to work, but it is a bit of dance. Basically you need to send a silent notification to the device you don't want to send you a notification and then get that device to check the list of devices it shouldn't send notifications too BEFORE sending the notification.

    The willPresent works too, perfectly... but as mentioned only if your application is running in the foreground. Obviously it has to check its list of banned devices too.

    0 讨论(0)
提交回复
热议问题