swiftmacosnotificationsauthorizationunusernotificationcenter

Unable to request authorization for local notifications on macOS


When requesting authorization for local notifications using:

do {
    _ = try await current().requestAuthorization(options: [.alert])
} catch let error {
    print(error)
}

I always get the error message:

Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application" UserInfo={NSLocalizedDescription=Notifications are not allowed for this application}

If I go to

System preferences > Notifications & Focus > My app name

I can see that they are disabled, but I never disabled them myself and I can't find a way to reset the setting.


Using Swift, macOS 12 Monterey, and the updated UserNotifications API using async/await.


Solution

  • Need to manually remove all traces of your app and try again from scratch, following all the steps listed here:

    After all these steps requesting authorization should work:

    do {
        _ = try await current().requestAuthorization(options: [.alert])
    } catch let error {
        print(error)
    }
    

    It only works once

    It will only work the first time, if you don't answer the notification or if you don't allow them you will need to repeat all the steps to try once more.