I've to develop a tiny program that send a local notification in on my Mac. This notification must be sent every 30 sec, so I create a command line tool with Xcode and I choose to develop in Objective-C. Xcode shows me the main.m file, in which I wrote the following code:
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;
}
where Notification it's a class in which I start a timer and I send the notification, the code is:
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
when I run the app it doesn't calls the selector method and it finish to run, what's wrong in my little program? Can you help me to fix it?
Thank you
AppleScript
I create a tiny AppleScript and it's working quite well, the code I used is the following:
on idle
display notification "Lorem ipsum dolor sit amet" with title "Title"
return 30
end idle
then I created an app from this script and it will be execute at the startup of the system and it's sending me a notification every 30 sec. I'm not sure if it's the right way to do that, but it's working...
LOOK FROM HERE
Objective-C
I've try to update my code as follow:
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];
}
but it doesn't send me the notification...