How to detect host app state in a Notification Service Extension?

772 Views Asked by At

A Notification Service Extension downloads a media file to display it as attachment of the UNNotification. However when the host app is in foreground, no notification is displayed, so it is unnecessary to for the notification extension to download the media file.

How can the download be prevented, depending on the app state?

1

There are 1 best solutions below

1
On

First from ios 10 you can present it with UNUserNotificationCenterDelegate delegate method

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

      completionHandler([.alert, .sound])


}

second it may be strange but you can share app state with userDefaults if you have app groups enabled with the extension