'Is it possible to set default alert style for macOS UNNotification?

My application posts local macOS notifications. It's important that these are not banner style but alert style instead.

So far I have achieved that by setting NSUserNotificationAlertStyle=alert in the Info.plist. When a first NSUserNotification is sent to the user, he is asked to approve the app's alerts. Once approved, the App gets Alert style set to Alerts in the macOS Preferences.

However, the NSUserNotification API s deprecated since macOS 11.

In the code below I've tried to use the newer UNNotification API, the Alert style is always set to banner no matter the NSUserNotificationAlertStyle property value.

Is there any way to work around that? Since users can change the alert style anyway, I am not sure why the apps cannot decide on the default style themselves.

import UserNotifications
import SwiftUI

@main
struct UNNotificationIssuesApp: App {

    init() {
        let notificationCenter = UNUserNotificationCenter.current()

        notificationCenter.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            if granted {
                notificationCenter.getNotificationSettings { settings in
                    print("Granted app notification permissions: \(settings)")
                }
            } else {
                print("All the app's notification permissions were revoked")
            }

            if let err = error {
                print("An error occurred while granting app notification permissions:", err.localizedDescription)
            }
        }
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Default banner alert style



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source