Posted By

rabc on 02/24/11


Tagged

notification background iphone multitask ios


Versions (?)

Schedule app local notification


 / Published in: Objective C
 

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

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

  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. }

Report this snippet  

You need to login to post a comment.