1

I want to develop a command line tool for notifications, like https://github.com/julienXX/terminal-notifier and use new UserNotification API

But now the program is throw a exception:

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

I don't know under what circumstances the Notification Center will throw this exception, and I would like to ask what configuration needs to be added to use the Notification Center in the command line tool.

My code:

import Cocoa
import UserNotifications

class AppDelegate: NSObject, NSApplicationDelegate {


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        let content = UNMutableNotificationContent()
        content.title = "title"
        content.body = "body"
        content.userInfo = ["method": "new"]
        content.sound = UNNotificationSound.default
        content.categoryIdentifier = "NOTIFICATION"

        let testCategory = UNNotificationCategory(identifier: "NOTIFICATION",
                                                  actions: [],
                                                  intentIdentifiers: [],
                                                  hiddenPreviewsBodyPlaceholder: "",
                                                  options: .customDismissAction)

        let request = UNNotificationRequest(identifier: "NOTIFICATION_REQUEST",
                                            content: content,
                                            trigger: nil)

        // Schedule the request with the system.
        let center = UNUserNotificationCenter.current()
        center.setNotificationCategories([testCategory])
        center.add(request) { (error) in
            if error != nil {
                // Handle any errors.
                print(error)
            }
        }
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
        return true
    }

}


let app = NSApplication.shared
let appDelegate = AppDelegate()
app.delegate = appDelegate
app.run()
Illuxiza
  • 11
  • 2
  • Does this answer your question? [Mac Mountain Lion send notification from CLI app](https://stackoverflow.com/questions/11712535/mac-mountain-lion-send-notification-from-cli-app) – Willeke Jan 13 '22 at 08:51
  • 1
    @Willeke No, my command line tool has bundle info, use NSUserNotification can work. But NSUserNotification is deprecated, so I want to use UNUserNotification. – Illuxiza Jan 14 '22 at 06:05
  • See [Asking Permission to Use Notifications](https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications) – Willeke Jan 14 '22 at 12:39
  • @Willeke I see UNNotificationSettings notificationCenterSetting: NotSupported, Does it have anything to do with this? – Illuxiza Jan 20 '22 at 06:29
  • Have you tried `requestAuthorization`? – Willeke Jan 20 '22 at 07:36
  • @Willeke Yes, it return error "Notifications are not allowed for this application" and granted is false – Illuxiza Jan 21 '22 at 08:04
  • @Illuxiza, did you ever figure out what was wrong ? – Jimmy Aug 14 '22 at 15:30

0 Answers0