Changes to light/dark mode aren't detectable within a notification service extension unless the phone is restarted

UPDATE: So I discovered that if UIScreen.main.traitCollection.userInterfaceStyle is called in a notification service extension then it doesn't detect any change to the iPhones light/dark mode unless the phone is restarted. (I tried with other extensions, changes are detected immediately in other extensions, however not with a notification service extension nor in a notification content extension).

ORIGINAL POSTING: I've got a notification service extension which is populating the notification with images before it's displayed.

The images are part of an image set with different images for light and dark modes. What I've discovered is that which image is displayed in the notification depends upon whatever mode the phone was in when the app was installed or when the phone was restarted and it will stay like that for any subsequent posted notifications forever regardless of what the phone's light/dark mode setting is, unless the phone is restarted.

Here's an example to illustrate what I mean, here's an image set in the Media.xcassets for the extension, its called "Grunt".

And here's some code in the extension:

        notificationContent!.title = "GRUNT"
        if let url = URL(forImageResource: "Grunt") {
            do {
                let attachment = try UNNotificationAttachment(identifier: "imageAttachment", url: url)
                notificationContent!.attachments = [attachment]
            } catch {
                NSLog("error")
            }
        }
        contentHandler(notificationContent!)

When the app is installed, if the phone is set to light mode, then when a notification is posted the 2x Light image is displayed in the notification. If the phone's setting is then changed to dark, then the 2x Light image still continues to displayed in all subsequent notifications that get posted.

Conversely, if the phone is set to dark mode when the app is installed then the 2x Dark image is displayed in a notification, similarly if the phone's settings are changed to Light, the Dark image continues to be displayed for any new notifications posted.

Until the phone is restarted - then any new notification display in accordance with whatever the phone's dark mode is set to at the time the phone is restarted.

In other words, if the phone's light/dark mode setting is changed, new notifications posted don't display the appropriate light/dark image from the image set unless the phone is restarted.

(Occurs with both iOS 16 and 17)

  • And here's another problem with notification service extensions is if it has the entitlement com.apple.developer.usernotifications.filtering then it can NEVER detect the light/dark mode because com.apple.developer.usernotifications.filtering ALWAYS returns light. This isn't the first time I've seen com.apple.developer.usernotifications.filtering interfere with things, this is now the 3rd bug I've found that only manifests when a NSE has this entitlement.

Add a Comment

Replies

Where did you find the URL(forImageResource: "Grunt") initializer?