通过Xcode命令行工具发送本地通知

时间:2015-01-14 09:39:26

标签: objective-c xcode

我要开发一个小程序,在我的Mac上发送本地通知。此通知必须每30秒发送一次,因此我使用Xcode创建命令行工具,我选择在Objective-C中进行开发。 Xcode向我展示了main.m文件,其中我编写了以下代码:

的main.m

#import <Foundation/Foundation.h>
#import "Notification.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Notification *notification = [[Notification alloc]init];
        [notification startNotification];
    }
    return 0;
}

其中Notification是一个我启动计时器并发送通知的类,代码是:

Notification.h

@interface Notification : NSObject <NSUserNotificationCenterDelegate>

- (void) startNotification;

@end

Notification.m

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "Notification.h"

@implementation Notification : NSObject

- (void) startNotification {
    NSTimer *timer = [[NSTimer alloc]init];
    if (timer == nil) {
        timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(sendNotification) userInfo:nil repeats:YES];
    }
}

- (void)sendNotification {
    NSUserNotification *notification = [[NSUserNotification alloc]init];
    notification.title = @"Prova";
    notification.informativeText = @"Ciao Salame";
    notification.soundName = NSUserNotificationDefaultSoundName;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications];

    NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
    [center scheduleNotification:notification];
    [center setDelegate:self];
}

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
    NSAlert *alert = [[NSAlert alloc]init];
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:notification.title];
    [alert setInformativeText:notification.informativeText];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert runModal];
    [center removeDeliveredNotification:notification];
}

@end

当我运行应用程序时,它不调用选择器方法并且它完成运行,我的小程序出了什么问题?你能帮我解决一下吗?

谢谢

的AppleScript

我创建了一个很小的AppleScript,它工作得很好,我使用的代码如下:

on idle

    display notification "Lorem ipsum dolor sit amet" with title "Title"

    return 30

end idle

然后我从这个脚本创建了一个应用程序,它将在系统启动时执行,并且每隔30秒向我发送一个通知。我不确定这是否是正确的方法,但它正在发挥作用......

从这里看

目标C

我尝试按以下方式更新我的代码:

Notification.m

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "Notification.h"

@implementation Notification : NSObject

- (void) startNotification {
    NSUserNotification *notification = [[NSUserNotification alloc]init];
    notification.title = @"Prova";
    notification.informativeText = @"Ciao Salame";
    notification.soundName = NSUserNotificationDefaultSoundName;

    [[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications];

    NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
    [center scheduleNotification:notification];
    [center setDelegate:self];
    sleep(30);

}

- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
    NSAlert *alert = [[NSAlert alloc]init];
    [alert addButtonWithTitle:@"OK"];
    [alert setMessageText:notification.title];
    [alert setInformativeText:notification.informativeText];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert runModal];
    [center removeDeliveredNotification:notification];
}

但它没有向我发送通知......

1 个答案:

答案 0 :(得分:0)

原因是,您的应用在实例化Notification后直接返回。只需调用startNotification即可阻止该应用返回。可能你想让它在后台运行一个线程。