How to hide a specific notification banner when application is in killed state and in background state in swift

36 Views Asked by At

In my project Pushy pod used to handle Firebase push notification. Now a requirement is that. Not to show notification banner when Application is in killed state or application is in background state.

I am able to handle this when application is in foreground.

func notificationWillPresent(userInfo: [String: Any], completionHandler: ((UNNotificationPresentationOptions) -> Void)?) {
    let remoteNotification = RemoteNotificationModel(userInfo)
    completionHandler?([.sound])
}

But I want this same when app is in background or in killed state.

1

There are 1 best solutions below

2
Piepants On

You are currently sending a user push notification, if you change it to an app directed notification then the OS will not deliver it to the app if its terminated anyway.

If the app is in the foreground and it receives the push, then turn it into a location notification and post it.

If the app is in the background and it receives the push then you can check if the app is in the foreground or background and therefore not turn the push into a local notification.

Or you can implement a notification service extension to intercept the push, and apply for the entitlement that will permit the extension to suppress the notification (this has to be requested through the Apple account web site)