title: Send a local notification by Xcode command line tool
body: <p>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:</p>
  
  <p><strong>main.m</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "Notification.h"
  
  int main(int argc, const char * argv[]) {
      @autoreleasepool {
          Notification *notification = [[Notification alloc]init];
          [notification startNotification];
      }
      return 0;
  }
  </code></pre>
  
  <p>where Notification it's a class in which I start a timer and I send the notification, the code is:</p>
  
  <p><strong>Notification.h</strong></p>
  
  <pre><code>@interface Notification : NSObject &lt;NSUserNotificationCenterDelegate&gt;
  
  - (void) startNotification;
  
  @end
  </code></pre>
  
  <p><strong>Notification.m</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import &lt;Cocoa/Cocoa.h&gt;
  #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
  </code></pre>
  
  <p>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?</p>
  
  <p>Thank you</p>
  
  <p><strong>AppleScript</strong></p>
  
  <p>I create a tiny AppleScript and it's working quite well, the code I used is the following:</p>
  
  <pre><code>on idle
  
      display notification "Lorem ipsum dolor sit amet" with title "Title"
  
      return 30
  
  end idle
  </code></pre>
  
  <p>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...</p>
  
  <p><strong>LOOK FROM HERE</strong></p>
  
  <p><strong>Objective-C</strong></p>
  
  <p>I've try to update my code as follow:</p>
  
  <p><strong>Notification.m</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import &lt;Cocoa/Cocoa.h&gt;
  #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];
  }
  </code></pre>
  
  <p>but it doesn't send me the notification...</p>