Skip to content

Instantly share code, notes, and snippets.

@RabbitMC
Created April 3, 2016 12:49
Show Gist options
  • Save RabbitMC/83f22c1b4295483548dc7bf44bd9c093 to your computer and use it in GitHub Desktop.
Save RabbitMC/83f22c1b4295483548dc7bf44bd9c093 to your computer and use it in GitHub Desktop.
iOS Local Notification Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *userNotificationSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil];
[application registerUserNotificationSettings:userNotificationSettings];
}
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSDate *alarmtime = [[NSDate date] dateByAddingTimeInterval:60*60*24]; // 24 hours (60*60*24) after closing the application
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *localNotificationAlarm = [[UILocalNotification alloc] init];
if (localNotificationAlarm) {
localNotificationAlarm.fireDate = alarmtime;
localNotificationAlarm.timeZone = [NSTimeZone defaultTimeZone];
localNotificationAlarm.repeatInterval = 0;
localNotificationAlarm.alertBody = @"Body Notification";
localNotificationAlarm.alertTitle = @"Title Notification";
[app scheduleLocalNotification:localNotificationAlarm];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
NSArray *oldNotifications = [app scheduledLocalNotifications];
if (oldNotifications.count > 0) {
[app cancelAllLocalNotifications];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment