Files
phonegap-plugins/iOS/LocalNotifications
Drew Dahlman c64d17d5a7 Cleanup
2012-04-15 19:38:39 -06:00
..
2012-04-15 19:38:39 -06:00

PHONEGAP LOCALNOTIFICATION The Phonegap LocalNotification plugin is great, but the documentation is lacking - also explanation of how to do more than set a 60 second timer.

This example goes through in detail how to set a timer for the future based on hours and minutes, as well as days in the future - also setting up repeat events for daily, weekly, monthly, yearly.

It also explains how to create a callback to your app when it is launched from that notification.

the full write up is here:
http://www.drewdahlman.com/meusLabs/?p=84

NOTES:
A breakdown of options -

  • date ( this expects a date object )
  • message ( the message that is displayed )
  • repeat ( has the options of 'weekly','daily','monthly','yearly')
  • badge ( displays number badge to notification )
  • foreground ( a javascript function to be called if the app is running )
  • background ( a javascript function to be called if the app is in the background )
  • sound ( a sound to be played, the sound must be located in your project's resources and must be a caf file )

ADJUSTING AppDelegate
After you've added LocalNotifications to your plugins you need to make a minor addition to AppDelegate.m

	// ADD OUR NOTIFICATION CODE
	- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
	{

	    UIApplicationState state = [application applicationState];
	    if (state == UIApplicationStateInactive) {
	        // WAS IN BG
	        NSLog(@"I was in the background");

	        NSString *notCB = [notification.userInfo objectForKey:@"background"];
	        NSString * jsCallBack = [NSString 
	                                 stringWithFormat:@"%@", notCB]; 
	        [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack];         

	        application.applicationIconBadgeNumber = 0;

	    }
	    else {
	        // WAS RUNNING
	        NSLog(@"I was currently active");

	        NSString *notCB = [notification.userInfo objectForKey:@"forground"];
	        NSString * jsCallBack = [NSString 
	                                 stringWithFormat:@"%@", notCB]; 


	        [self.viewController.webView  stringByEvaluatingJavaScriptFromString:jsCallBack];

	        application.applicationIconBadgeNumber = 0;
	    }                 
	}

Add this code to the end of your AppDelegate.m file in order for the callback functions to work properly!

EXAMPLE

window.plugins.localNotification.add({
	date: d, // your set date object
	message: 'Hello world!',
	repeat: 'weekly', // will fire every week on this day
	badge: 1,
	foreground:'app.foreground',
	background:'app.background',
	sound:'sub.caf'
});