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()