Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active December 16, 2015 01:19
Show Gist options
  • Select an option

  • Save lamprosg/5354257 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5354257 to your computer and use it in GitHub Desktop.
(iOS) Local Notifications
- (IBAction)addNotification:(id)sender {
//Create the localNotification object
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//Set the date when the alert will be launched using the date adding the time the user selected on the timer
localNotification.fireDate:[NSDate dateWithTimeIntervalSinceNow:[datePicker countDownDuration]];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//The button's text that launches the application and is shown in the alert
localNotification.alertAction:@"Launch";
//Set the message in the notification from the textField's text
localNotification.alertBody:[alertBodyField text];
localNotification.soundName = UILocalNotificationDefaultSoundName;
//Set that pushing the button will launch the application
[localNotification setHasAction: YES];
//Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1
localNotification.applicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1;
//Schedule the notification with the system
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
//Set the alertNotification to be shown showing the user that the application has registered the local notification
alertNotification.hidden:NO;
/********************************/
//Cancel all notifications
// Cancel all Notifications
UIApplication* app = [UIApplication sharedApplication];
NSArray *notifications = [app scheduledLocalNotifications];
if ([notifications count] > 0)
[app presentLocalNotificationNow:notifications[0]];
//OR
[[UIApplication sharedApplication] cancelAllLocalNotifications];
//If there is a badge number, reset it in the delegate's didfinshlaunching
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
// Reset the Icon Alert Number back to Zero
application.applicationIconBadgeNumber = 0;
// Detect the Notification after a user taps it
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
NSLog(@"Recieved Notification %@",localNotif);
}
..
}
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notification when the app is running
NSLog(@"Recieved Notification %@",notif);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment