iOS: Simple Local Notification 3 Days Prior - ios

I wrote an app that allows the user to enter a date on a date picker, and then when they tap a button it will schedule a local notification. The only issue is, the notification fires right when I tap the button. Any help is much appreciated! Here is my code:
- (IBAction)scheduleNotifButton:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [self.datePicker date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-3];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = targetDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in 3 days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

You need to use [NSCalendar dateByAddingComponents:toDate:options:] instead:
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [self.datePicker date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-3];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
[dateComponents release];
...

If you're looking for complete code on how to schedule a notification a bit later (like say 3 seconds) here's the complete code :
NOTE : If you're inside the app you won't see a message box at the top of the screen, that might have to be handled through the UIApplication delegate.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [[NSDate alloc] init];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setSecond: 3 ];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
localNotification.fireDate = targetDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Notified";
localNotification.alertAction = #"Show";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Related

iOS: Is it possible to launch an update in background of an app when iPad is in standby mode (when user has pressed the power button quickly)?

I have a piece of code that allows me to update my app at a specific time:
MainViewController.m:
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
[components setHour:14];
[components setMinute:5];
[components setSecond:0];
NSDate *nextAlarm = [calendar dateFromComponents:components];
if ([nextAlarm timeIntervalSinceNow] < 0)
{
nextAlarm = [nextAlarm dateByAddingTimeInterval:60*60*24];
}
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setRepeatInterval: NSCalendarUnitDay];
[localNotification setFireDate:nextAlarm];
[localNotification setAlertBody:#"It's been one day"];
[localNotification setValue:nextAlarm forKey:#"fireDate"];
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
AppDelegate.m
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *synchroDate = notification.fireDate;
NSDate *currentDate= [NSDate date];
NSLog(#"difference : %f", [synchroDate timeIntervalSinceDate:currentDate]);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute);
NSDateComponents *date1Components = [calendar components:comps
fromDate: currentDate];
NSDateComponents *date2Components = [calendar components:comps
fromDate: synchroDate];
currentDate = [calendar dateFromComponents:date1Components];
synchroDate = [calendar dateFromComponents:date2Components];
BOOL result = [currentDate isEqual:synchroDate];
if (result)
{
[self autoSynchro];
}
My problem is that I would like that code be launched when my device is off. I can see that the notification is well launched on the ipad main screen, but the function is not launched...
May be I have to use that:
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateInactive)
{
//Do checking here.
}
But how to launch it if it is in standby mode?
Any ideas?

Local notification with a specific day in iOS8

I want display local notification on my app in a specific day, for example, I want the notification is displayed all the 1 of the month.
I have this code, how can I customize it ?
//LOcal Notification
NSDate *today = [NSDate new];
NSCalendar *gregorian =
[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [NSDateComponents new];
[offsetComponents setDay:???];
[offsetComponents setHour:10];
[offsetComponents setMinute:0];
NSDate *thedate = [gregorian dateByAddingComponents:offsetComponents
toDate:today options:0];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = thedate;
localNotification.alertBody = #"Hey, il serait temps de faire un point non ? \ue104";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
//END
There is too a depreciated error with ios8 on the gregorian calendar, how can I resolve it ?
//assuming you want to fire the notifiaction on 1st of every month.
NSInteger desiredWeekday = 1;// it specifies on which day of month
NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSWeekdayCalendarUnit];
NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSInteger currentWeekday = dateComponents.weekday;
NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek;
NSDateComponents *daysComponents = [[NSDateComponents alloc] init];
daysComponents.day = differenceDays;
NSDate *resultDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:[NSDate date] options:0];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = resultDate;
localNotification.alertBody = #"Hey, il serait temps de faire un point non ? \ue104";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSMonthCalendarUnit;// repeat notifiaction after a month
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

ios. Disable everyday notification if it is already irrelevant

I want to remind user to take photo everyday. I use code for every day local push notificatons
UILocalNotification *everyDayNotification = [[UILocalNotification alloc] init];
everyDayNotification.repeatInterval = NSDayCalendarUnit;
NSDate *currentDate = [[NSDate alloc] init];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
int hour = [hours_string intValue];
int minutes = [minutes_string intValue];
[components setHour:hour];
[components setMinute:minutes];
NSDate *today10am = [calendar dateFromComponents:components];
everyDayNotification.fireDate = today10am;
everyDayNotification.timeZone = [NSTimeZone defaultTimeZone];
everyDayNotification.alertBody = #"It us time";
everyDayNotification.alertAction = #"Action";
everyDayNotification.soundName = UILocalNotificationDefaultSoundName;
everyDayNotification.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"EveryDay"
forKey:#"RemindNotificaion"];
everyDayNotification.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:everyDayNotification];
But if the user has made the photo before it is time to notice that. I want to skip the notification for today. Is that possible?
I would suggest making an individual UILocalNotification for each day and storing them, perhaps in an array in NSUserDefaults. Then you can cancel a notification with the following:
[[UIApplication sharedApplication] cancelLocalNotification:notficiationInstance];

Local notifications are repeat on every day and every month

I have a Local Notifications. Like this ;
NSNumberFormatter *faturaSonOdemeGunuFormatter = [[NSNumberFormatter alloc] init];
[faturaSonOdemeGunuFormatter setNumberStyle:NSNumberFormatterNoStyle];
NSNumber *faturaSonOdemeGunuNumber = [faturaSonOdemeGunuFormatter numberFromString:_txtFaturaGunu.text];
NSDate *currentDate = [NSDate date];
NSCalendar * calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:currentDate];
[components setTimeZone:[NSTimeZone systemTimeZone]];
[components setHour:12];
[components setMinute:40];
[components setDay: faturaSonOdemeGunuNumber.integerValue -3];
NSDate *test = [calendar dateFromComponents:components];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = test;
localNotification.alertBody = [NSString stringWithFormat:#"%# Faturanıza 3 gün kaldı.",_txtFaturaAdi.text];
localNotification.alertAction = #"Faturayı göster";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
This code does not repeat itself.I want to repeat Local Notification every Month in faturaSonOdemeGunuNumber integerValue day.It should be start sonOdemeGunuNumber -3 (for example user writes 17. it should start 14) and it should give a notification on every day until faturaSonOdemeGunuNumber. I mean it should continue notify the users every day until selected day.
How can i do that?
Thank you!
Add this:
localNotification.repeatInterval = kCFCalendarUnitMonth; //For monthly repeats
localNotification.repeatInterval = kCFCalendarUnitDay; //For daily repeats
Try this,
notification.repeatInterval= NSDayCalendarUnit; //For day
notification.repeatInterval= NSWeekCalendarUnit; //For Week
notification.repeatInterval= NSMonthCalendarUnit; //For Month
Ref Link: http://useyourloaf.com/blog/2010/09/13/repeating-an-ios-local-notification.html

iOS – UILocalNotification fired twice for same notification

If I schedule two UILocalNotifications and set them both to fire at the exact same fireDate. Then on the device (this is not the simulator bug) on the fireDate the application:didReceiveLocalNotification: will fire 4 times (2 times for each notification). Is this a known bug? Because I have not been able to find any information about it.
Please report the bug to http://bugreport.apple.com.
Having said that, it has been noticed before that while there is the bug in the simulator there also appears to be a bug on the device.
See the comments and answers on this SO question: local notification "didReceiveLocalNotification" calls twice
try this it's work in my application :
-(IBAction)setRemind:(id)sender{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//Gets our picker
NSDate *selectedTime = [datePicker date];
strDate2 = [dateFormatter2 stringFromDate:selectedTime];
NSDate *Date=[dateFormatter2 dateFromString:strDate2];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:Date];
// Set up the fire time
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
[dateComp setDay:[dateComponents day]];
[dateComp setMonth:[dateComponents month]];
[dateComp setYear:[dateComponents year]];
[dateComp setHour:9];
[dateComp setMinute:00];
[dateComp setSecond:00];
[dateComp release];
NSDate *date = [calendar dateFromComponents:dateComp];
[self scheduleAlarmForDate:date message:txtDescri.text];
}
-(IBAction)scheduleAlarmForDate:(NSDate*)date message:(NSString*)msg
{
//====== TO SEE OLD NOTIFI=======
UIApplication *Ap = [UIApplication sharedApplication];
NSArray *arr = [Ap scheduledLocalNotifications];
NSLog(#"Old Notifications :>> %#",arr);
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification *alarm = [[UILocalNotification alloc] init];
// Create a new notification
alarm.fireDate = date;
NSLog(#"fireDate IS >> %#", alarm.fireDate);
alarm.timeZone = [NSTimeZone localTimeZone];
alarm.alertBody = msg;
NSLog(#"msg IS >> %#",msg);
alarm.alertAction = #"Show";
alarm.repeatInterval = 0;
alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.applicationIconBadgeNumber = 1;
[app scheduleLocalNotification:alarm];
[alarm release];
}
i Hope it's helpful to you.

Resources