iosxcodeapple-push-notificationsunnotificationserviceextension

Can handler not show remote notification when iOS app is killed?


My requirement is prevent remote notification from being display.
I can handler this if app is in foreground or background in following methods:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        //logic hide notification here
    }
public func userNotificationCenter(_ center: UNUserNotificationCenter,
                                       willPresent notification: UNNotification,
                                       withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        //logic hide notification here
    }

When app is being killed, I'm trying to handler logic in UNNotificationServiceExtension but it did not work:

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        // logic hide notification here
    }

I've read this article: Is it possible to prevent a remote notification from being displayed? It said that Apple not allow to do this, is this correct?


Solution

  • Yes, it's correct.

    You cannot catch a remote notification and hide it. The best way to not receive remote notifications is to say no to the permission. If your server send remote notifications you can configure it not to send notifications when you does not want.

    Another way to send notifications and not show them is to send "silent notification". It allows you to receive the notification, get its content and do what you want (you have only 30s). I thinks, you can send silent notifications, check content and send a local notification if it needs to, but I'm not sure.