Mac Mountain Lion 从 CLI 应用程序发送通知 [英] Mac Mountain Lion send notification from CLI app
问题描述
如何从命令行应用程序向通知中心发送通知?到目前为止,我的尝试编译并运行,但没有成功通知我.
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.
示例
#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;
}
然后我编译如下:
clang -framework cocoa /tmp/Notes.m
我得到
2012-07-29 16:08:35.642 a.out[2430:707] Running notifications
作为输出,但没有通知:(
as output, but no notification :(
协同设计是其中的一个因素吗?
Is codesigning a factor in this?
推荐答案
我发现 NSUserNotificationCenter
在 [[NSBundle mainBundle] bundleIdentifier]
返回正确的标识符时起作用.所以,我写了一些 swizzling 的代码,你可以在 https://github.com/norio-nomura/usernotification 找到
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
它可以在没有应用程序包的情况下发送 NSUserNotification
.
It can send an NSUserNotification
without an Application Bundle.
这篇关于Mac Mountain Lion 从 CLI 应用程序发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!