[英]Send a local notification by Xcode command line tool
我必须开发一个微型程序,以便在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秒发送一次通知。 我不确定这是否是正确的方法,但是它在起作用...
从这里看
Objective-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];
}
但它没有向我发送通知...
原因是,您的应用程序在实例化Notification
之后直接返回。 仅调用startNotification
不会阻止应用返回。 可能您想让它在后台的线程内运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.