I try to using the module UserNotifications
in swift project, to send notifications of my macos app.
I pack my app with nested bundle like:
/Application/MyApp.app
- Contents
- Resources
- Frameworks
- MacOS
- my_binary
- Notifier.app
- Resources
- Frameworks
- Contents
- MacOS
- notifier_binary
When I use cmdline to execute my notifier_binary, it's work fine.
But when I execute it form my_binary, it will failed sometimes in api requestAuthorization
, and the auth setting result will be not authed.
But in current user's setting, it had been allowed.
The error message in log, I can't find anything useful in google result, I have no idea what this error message means.
2022-10-27 16:32:11.297912+0800 0x3ded Error 0x0 1697 0 notifier_binary: (UserNotifications) [com.apple.UserNotifications:Connections] [com.my.notifier] Getting notification settings failed with error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.usernotifications.usernotificationservice was invalidated: failed at lookup with error 3 - No such process." UserInfo={NSDebugDescription=The connection to service named com.apple.usernotifications.usernotificationservice was invalidated: failed at lookup with error 3 - No such process.}
Here it's my swift code:
class NotiSender {
let un = UNUserNotificationCenter.current()
func sendNoti(){
var waiting = true;
un.requestAuthorization(options: [.alert, .sound]) {(authorized, error) in
if authorized {
} else if !authorized {
Logger.error("user not authorized\n")
} else {
Logger.error("error: \(error?.localizedDescription)\n")
}
waiting = false;
}
while (waiting) {
sleep(1);
}
un.getNotificationSettings{ (settings) in
if settings.authorizationStatus == .authorized {
// send my notifications
.......
}
}
@main
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
UNUserNotificationCenter.current().delegate = self
let sender = NotiSender()
sender.sendNoti()
......
}
}
Did I miss some entitlements or app setting in Info.plist ? Or did I misuse the api?
Thanks for any help!!!