Mac OSX上的NSNotifier

时间:2014-05-23 22:41:46

标签: objective-c macos

我从头开始构建一个mac守护进程。

这是代码的简化版本。

#import <stdio.h>
#import <stdlib.h>
#import <Foundation/Foundation.h>
#include "notifier.h"

    int new_notification();

    notifier *not;

    int main () {
            @autoreleasepool {
                    not = [[[notifier alloc] init] autorelease];
                    pid_t pid;
                    pid = fork();
                    if(pid > 0) {
                            printf("my child id is %d\n", pid);
                            exit(0);
                    }
                    while(1) {
                            int n = new_notification();
                            if(n > 0) {
                                    NSUserNotification *notification = [[NSUserNotification alloc] init];
                                    notification.title = @"Hello, World!";
                                    notification.informativeText = @"A notification";
                                    notification.soundName = NSUserNotificationDefaultSoundName;
                                    [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];

                                    printf("new notification : count = %d !!\n", n);
                            }
                            sleep(1);
                    }
            }
            return 0;
    }



    int new_notification() {
            return [not get_notifications];
    }

我在窗口看不到通知,但我认为我必须将我的应用程序设为&#34; key&#34;应用程序,如果是这样,它是如何做到的?我可以看到终端上的输出,并检查是否

    (NSClassFromString(@"NSUserNotificationCenter")==nil)
            I get FALSE

1 个答案:

答案 0 :(得分:1)

我希望回答你的问题的扩展评论...

如果您的应用处于有效状态,则无法显示通知。但是您可以在通知中心找到它。 (通知会引起用户对他们尚未查看的应用的关注。)

我建议您尝试[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory]明确您的应用不在前台,但我不能完全确定[NSApplication sharedApplication]是否NSApp是你用守护进程的东西。

您的部分问题可能实际上是,如果不调用[NSApplication sharedApplication]并告诉run您永远不会成为应用,那么您就无法使用通知中心......或成为附件。但我也不确定。