spacitron
spacitron

Reputation: 2183

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

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?

Upvotes: 26

Views: 19282

Answers (5)

Naiyer Aghaz
Naiyer Aghaz

Reputation: 221

Try this code if you want to alert the particular type notification only,

I have added here to display the alert of two type of notification i.'tokenupdate' and ii.'notificationv1' rest of type will not display alert of notification.

Check below code, Hope this help you.

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let userInfo = notification.request.content.userInfo
   
    let type =  userInfo[AnyHashable("type")] as? String
    if type == "tokenupdate"{
        handleNotification(userInfo: userInfo)
        completionHandler([.alert,.badge,.sound])
    }
    else if  type == "notificationv1"{
        let deleteType =  userInfo[AnyHashable("AppointmentFlag")] as? String
        if deleteType?.replacingOccurrences(of:" ", with: "") == "B"{
            let ids =  userInfo[AnyHashable("id")] as? String
            AppointmentManager().deleteById(id: "\(ids!)")
        }
        completionHandler([.alert,.badge,.sound])
    }

  
  
}

Upvotes: 3

Landschaft
Landschaft

Reputation: 1337

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.

From iOS 13.3 the notification service entitlement com.apple.developer.usernotifications.filtering allows for filtering notifications (as pointed out in Aviharsh Shukla's comment).

Upvotes: 33

Claudiu
Claudiu

Reputation: 645

Starting from iOS 13.3 it is possible. You can prevent it from being displayed in User Notification Extension. All you need to do is to be granted from Apple the Notification Service Entitlement and set apns-push-type header field to alert.

You can check this for all the details: https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_usernotifications_filtering

Upvotes: 10

nalexn
nalexn

Reputation: 10791

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.

Upvotes: -1

jo.On
jo.On

Reputation: 776

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])
    }
}

Upvotes: 22

Related Questions