Skip to content

Instantly share code, notes, and snippets.

@Eridana
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save Eridana/d00bfde05fdf5dbbefcc to your computer and use it in GitHub Desktop.

Select an option

Save Eridana/d00bfde05fdf5dbbefcc to your computer and use it in GitHub Desktop.
Set local notification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
[[UIApplication sharedApplication] registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(@"Recieved Notification %@",localNotif);
application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Handle the notificaton when the app is running
if (notification) {
NSLog(@"Recieved Notification %@", notification);
application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}
}
- (void)setNotificationWithDate:(NSDate *)date
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:date];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:date];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) {
return;
}
localNotification.fireDate = itemDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss yyyy-MM-dd"];
// Notification details
localNotification.alertBody = [NSString stringWithFormat:@"Просмотр события в %@", [dateFormatter stringFromDate:date]];
// Set the action button
localNotification.alertAction = @"Смотреть";
//localNotification.
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotification.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment