I'm trying to implement push notifications on MacOS within a C codebase. Ideally, there'd just be one Objective-C file containing (1) a public C function I can call and (2) some Objective-C code I can use to throw a notification. This way, the source files can be compiled and linked seamlessly in the build process.
Toward this end, I've been trying to create a minimal example that can throw notifications with just a single .m
file (not an entire XCode project), much like the one discussed in NSUserNotificationCenter not showing notifications. However, two problems:
- I still can't get the code to work despite trying the solutions in the aforementioned link. It compiles and runs but does not throw a notification.
- If possible, we'd like to switch to the new user notifications API. Not a big deal if this isn't possible for now, though.
Here's what I've tried so far:
#import <Foundation/Foundation.h>
#import <Foundation/NSUserNotification.h>
@interface AppDelegate : NSObject <NSUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
- (void)throwNotification {
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"Some title";
userNotification.informativeText = @"Some text";
printf("trying to throw {%s %s}\n", [[userNotification title] UTF8String], [[userNotification informativeText] UTF8String]);
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}
@end
int main (int argc, const char * argv[]) {
AppDelegate *app = [[AppDelegate alloc] init];
[app throwNotification];
return 0;
}
This is compiled with cc -framework Foundation -o app main.m
.
Any insight would be appreciated!