NSUserNotificationCenter未显示通知

Loï*_*oix 1 nsusernotification nsusernotificationcenter

我不确定我是否出了问题,但是从我能找到的例子中可以看出来.我写了那个小小的helloworld.

#import <Foundation/Foundation.h>
#import <Foundation/NSUserNotification.h>


int main (int argc, const char * argv[])
{
    NSUserNotification *userNotification = [[NSUserNotification alloc] init];
    userNotification.title = @"Some title";
    userNotification.informativeText = @"Some text";

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我编译它:

cc -framework Foundation -o app main.m
Run Code Online (Sandbox Code Playgroud)

它编译,我可以执行它,但它不会显示任何东西.由于某些原因,没有任何东西出现.我读到如果应用程序是主要角色,则可能无法显示通知.如何让它显示通知?

小智 7

设置委托并覆盖

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center 
                               shouldPresentNotification:(NSUserNotification *)notification {
  return YES;
}
Run Code Online (Sandbox Code Playgroud)