I'm trying to throw notifications from a standalone Objective-C file. The NSUserNotification API will be deprecated after OSX 11, so I'm looking to switch to the newer UNUserNotification interface.
Unfortunately, I'm not able to find much on the topic from Googling. I have the following code that throws an error:
notif.m:
#import <stdio.h>
#import <Cocoa/Cocoa.h>
#import <UserNotifications/UserNotifications.h>
#import <objc/runtime.h>
int native_show_notification(char *title, char *msg) {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString stringWithUTF8String:title];
content.body = [NSString stringWithUTF8String:msg];
content.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"NOTIFICATION" content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
printf("NOTIFICATION SUCCESS ASDF");
}
}];
return 0;
}
int main() {
native_show_notification("Foo" , "Bar");
}
Info.plist in the same directory:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.microsoft.VSCode</string>
</dict>
</plist>
This is compiled using cc -framework Cocoa -framework UserNotifications -o app notif.m. The Info.plist is incorporated automatically, so there shouldn't be a bundling issue.
Unfortunately, after running ./app I get the following error:
Assertion failure in +[UNUserNotificationCenter currentNotificationCenter], UNUserNotificationCenter.m:54
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'bundleProxyForCurrentProcess is nil: mainBundle.bundleURL file:///path-to-folder-containing-the-source-files'
I'm new to MacOS/Objective-C development and am unable to parse this message. Couldn't understand things I could find on Google either. Any insights would be appreciated; thanks so much!