Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I send a notification to the notification center from a command line app? My attemps so far compile and run, but don't succeed in notifying me.

Example

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
    NSLog(@"Running notifications");

    NSUserNotification *note = [[NSUserNotification alloc] init];
    [note setTitle:@"Test"];
    [note setInformativeText:@"Woot"];

    NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
    [center scheduleNotification: note];

    return 0;
}

I then compile like:

clang -framework cocoa /tmp/Notes.m

and I get

 2012-07-29 16:08:35.642 a.out[2430:707] Running notifications

as output, but no notification :(

Is codesigning a factor in this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
393 views
Welcome To Ask or Share your Answers For Others

1 Answer

I found that NSUserNotificationCenter works when [[NSBundle mainBundle] bundleIdentifier] returns the proper identifier. So, I wrote some swizzling code that you can find at https://github.com/norio-nomura/usernotification

It can send an NSUserNotification without an Application Bundle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...