Reputation: 645
I feel like I am going insane here.
I'm making a little command-line tool to let me know when a Twitch channel I like goes live. So far, everything works. There's only one, small issue: [NSUserNotificationCenter defaultUserNotificationCenter]
is consistently returning nil
, no matter where I call it in the program. This is a bit of a problem, since I need to be able to send notifications.
After a solid hour of googling, I can't figure out what's going on. Something like [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:myDelegate]
doesn't crash, but I'm pretty sure that's because you can send messages to nil
in Obj-C. Similarly, I can do something like [[NSUserNotificationCenter defaultNotificationCenter] deliverNotification: mynote]
, but nothing is displayed onscreen (and this is with the delegate for [NSUserNotificationCenter defaultUserNotificationCenter]
set to return YES
for the userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)
method.)
Additionally, all notifications presented via deliverNotification
do not have their delivered
property set to YES.
The only thing I can think of is that the usage of NSUserNotificationCenter
only works if you're running an NSApplication
, but I cannot find that documented anywhere. I'm running a simple command-line tool, which is pretty much a main
function, an @autoreleasepool
, and some method calls.
Is there anything obvious that can cause [NSUserNotificationCenter defaultUserNotificationCenter]
to return nil?
Upvotes: 4
Views: 437
Reputation: 69027
The only thing I can think of is that the usage of NSUserNotificationCenter only works if you're running an NSApplication, but I cannot find that documented anywhere.
I don't think it would be easy to find it explicitly documented somewhere, but if your app is a simple command line tool, i.e., it is not using a NSRunLoop
, I doubt that features like notifications can work at all. They intrinsically require your program to have a run loop that sits there waiting for events (e.g., a notification coming in, a click, a touch) and dispatching them to whomever should handle them.
You might try starting and NSRunLoop
from you main function, but it would cease to be a simple command-line tool, I guess.
Upvotes: 2