Schedule app local notification


/ Published in: Objective C
Save to your folder(s)

Show a notification immediately after application enter in background (but you can schedule it too)


Copy this code and paste it in your HTML
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. NSLog(@"application: didFinishLaunchingWithOptions:");
  3. // Override point for customization after application launch
  4.  
  5. UILocalNotification *localNotif = [launchOptions
  6. objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
  7.  
  8. if (localNotif) {
  9. // has notifications
  10. }
  11. else {
  12. [[UIApplication sharedApplication] cancelAllLocalNotifications];
  13. }
  14. [window makeKeyAndVisible];
  15. return YES;
  16. }
  17.  
  18. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif {
  19. NSLog(@"application: didReceiveLocalNotification:");
  20.  
  21. }
  22.  
  23. - (void)applicationDidEnterBackground:(UIApplication *)application {
  24.  
  25. UILocalNotification *localNotif = [[UILocalNotification alloc] init];
  26. localNotif.fireDate = [NSDate date]; // show now, but you can set other date to schedule
  27.  
  28. localNotif.alertBody = @"this is a notification!";
  29. localNotif.alertAction = @"notification"; // action button title
  30.  
  31. localNotif.soundName = UILocalNotificationDefaultSoundName;
  32.  
  33. // keep some info for later use
  34. NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"item-one",@"item", nil];
  35. localNotif.userInfo = infoDict;
  36.  
  37. [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
  38. [localNotif release];
  39. }

URL: http://iphonesdkdev.blogspot.com/2010/04/local-push-notification-sample-code-os.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.