【发布时间】:2012-07-27 14:12:16
【问题描述】:
如何从命令行应用程序向通知中心发送通知?到目前为止,我的尝试编译并运行,但没有成功通知我。
例子
#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
作为输出,但没有通知:(
代码设计是其中的一个因素吗?
【问题讨论】:
-
该文档特别提到了“应用程序或帮助应用程序”,因此它可能拒绝来自命令行程序的请求。另外,看看使用
deliverNotification:是否有任何不同。 -
@KevinGrant 感谢您的建议,没有不同的结果。看起来你和 omz 对需要帮助应用程序的看法是正确的,但我会抱有更长的希望。
-
@KevinGrant 在一个有趣的注释上,
deliverNotification是我让它在 App 包中工作的唯一方法。很好的呼吁。
标签: objective-c cocoa nsnotificationcenter osx-mountain-lion nsusernotification