iOS create local notification when app exit with 10 days - ios

I am create a local notification with 10 days and at 15:30 when app exit. Here is a code:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
float day = 10;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*day];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: newDate];
[componentsForFireDate setHour: 15];
[componentsForFireDate setMinute:30];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDateOfNotification;
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
When I try change date & time in the my phone to the next day (now + 1), It's still show notification, next day, still show. I want local notification show each 10 days when app exit. Any suggests to fix this problems ?

try this code: your app open after 10 day notification fire and other 1 day fire.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsForFireDate = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate: [NSDate date]];
componentsForFireDate.day = componentsForFireDate.day + 10;
[componentsForFireDate setHour: 15];
[componentsForFireDate setMinute:30];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
NSLog(#"Date: %#",fireDateOfNotification);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDateOfNotification;
localNotification.alertBody = #"This is local notification!";
localNotification.repeatInterval = NSCalendarUnitDay;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
hope this code help you.

After changing the time in your system, please check the simulator time also. initially it not updated.
In simulator settings, General->language & region-> Advanced-> Automatica switch will be on mode. select off and on. then you simulator time will also change

Related

Show local notification on specific day and specific time in iOS

i want to show local notification on every saturday like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=#"EEEE";
NSString *dayString = [[dateFormatter stringFromDate:[NSDate date]] capitalizedString];
NSLog(#"day: %#", dayString);
if([dayString isEqualToString:#"Saturday"])
{
NSLog(#"Success");
[self PushNotification];
}
-(void)PushNotification
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertTitle = #"Test";
localNotification.alertBody =#"test of notification";
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [NSDate date]];
[componentsForFireDate setWeekday: 7]; //for fixing Saturday
[componentsForFireDate setHour: 17]; //for fixing 5PM hour
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
localNotification.repeatInterval = NSCalendarUnitWeekOfMonth;
}
but my local notification is display in every minit then how can i display notification on every Saturday of the week.
thanks.
Try to declare fireData property in localNotification and schedule your notification
localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Hope this help

5 local notifications every day

I want to have 5 local notifications every day and here is the code I used to trigger local notification, everything is cool, but if the first notification comes out, and if I run the APP foreground, than or the other notification will not come out. Please help to keep all the notifications even if I run the APP foreground. 😊
UILocalNotification* n1 = [[UILocalNotification alloc] init];
NSCalendar *c1 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co1 = [c1 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co1 setHour:15];
[co1 setMinute:54];
n1.fireDate = [c1 dateFromComponents:co1];
n1.alertBody = #"3:54";
n1.soundName = #"alarm.wav";
n1.timeZone = [NSTimeZone defaultTimeZone];
n1.alertAction= #"3:54";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
UILocalNotification* n2 = [[UILocalNotification alloc] init];
NSCalendar *c2 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co2 = [c2 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co2 setHour:15];
[co2 setMinute:55];
n2.fireDate = [c2 dateFromComponents:co2];
n2.alertBody = #"3:55";
n2.soundName = #"alarm.wav";
n2.timeZone = [NSTimeZone defaultTimeZone];
n2.alertAction= #"3:55";
[[UIApplication sharedApplication] scheduleLocalNotification: n2];
UILocalNotification* n3 = [[UILocalNotification alloc] init];
NSCalendar *c3 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co3 = [c3 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co3 setHour:15];
[co3 setMinute:56];
n3.fireDate = [c3 dateFromComponents:co3];
n3.alertBody = #"3:56";
n3.soundName = #"alarm.wav";
n3.timeZone = [NSTimeZone defaultTimeZone];
n3.alertAction = #"3:56";
[[UIApplication sharedApplication] scheduleLocalNotification: n3];
UILocalNotification* n4 = [[UILocalNotification alloc] init];
NSCalendar *c4 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co4 = [c4 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co4 setHour:15];
[co4 setMinute:57];
n4.fireDate = [c4 dateFromComponents:co4];
n4.alertBody = #"3:57";
n4.soundName = #"alarm.wav";
n4.timeZone = [NSTimeZone defaultTimeZone];
n4.alertAction = #"3:57";
[[UIApplication sharedApplication] scheduleLocalNotification: n4];
UILocalNotification* n5 = [[UILocalNotification alloc] init];
NSCalendar *c5 = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *co5 = [c5 components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[co5 setHour:15];
[co5 setMinute:58];
n5.fireDate = [c5 dateFromComponents:co5];
n5.alertBody = #"3:58";
n5.soundName = #"alarm.wav";
n5.timeZone = [NSTimeZone defaultTimeZone];
n5.alertAction = #"3:58";
[[UIApplication sharedApplication] scheduleLocalNotification: n5];
If your App is in foreground No notification is presented but this below method will be called,
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}
First you should identify the notification by userinfo attached in it.(if you haven't given, give userinfo to every notification.)
present any alert when you get a call in this method.

Weekday Local Notification Not Firing

I'm building an application that allows the user to have a local notification to repeat every weekday, because apple doesn't have a repeat interval for WeekDays I needed to create a local notification for every week day. I made a for loop looping 5 times the number of days in a work week. However it doesn't seem to be firing. But when I got to delete the notifications I see all five, the just wont fire. This is what my code looks like.
NSDate *today = [NSDate date];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = _alarmTime;
localNotification.alertBody = #"Wake up or pay-up!";
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.alertAction = #"Wake up or pay-up!";
localNotification.soundName = #"sound_ring.caf";
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = #"Everyday";
break;
case 2:
//Attempting to create 5 local notifications with different days but same time.
for (int i = 2; i <= 6; i++){
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(#"%D",i);
}
If anyone has any ideas please let me know! Thanks in advance.
Update:
NSDate *startDate = [gregCalendar dateFromComponents:dateComponent];
[dateComponent setMonth:0];
[dateComponent setDay:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setYear:0];
NSDate *fireDate = [gregCalendar dateByAddingComponents:dateComponent toDate:startDate options:0];
UILocalNotification *localNotification2 = [localNotification copy];
localNotification2.fireDate = fireDate;
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(#"%D",i);
Okay I was finaly able to get local notifications to repeat every weekday, I added [setHour] and [setMinute] as well as changed the int in the for loop to a NSUInteger. Here is my code below.
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:_alarmTime];
switch (_repeatInt) {
case 1:
localNotification.repeatInterval = NSCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
_repeatString = #"Everyday";
break;
case 2:
for (NSUInteger i = 2; i <= 6; i++) {
NSDateComponents *components = [gregCalendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:_alarmTime];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
[dateComponent setMinute:minute];
[dateComponent setWeekday:i]; // 2 = mon // 3= tues // 4 = wends // 5 = thurs // 6 = fri
[dateComponent setHour:hour];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = fireDate;
localNotification2.alertBody = #"Wake up or pay-up!";
localNotification2.timeZone = [NSTimeZone systemTimeZone];
localNotification2.alertAction = #"Wake up or pay-up!";
localNotification2.soundName = #"sound_ring.caf";
localNotification2.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
NSLog(#"%lu",(unsigned long)i);
}
They don't fire because the date you give them is invalid.
Here's how I generated the date for X number of days in the future:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *startDate = [calendar dateFromComponents:components];
[components setMonth:0];
[components setDay:X];
[components setYear:0];
NSDate *endDate = [calendar dateByAddingComponents:components toDate:startDate options:0];

UILocalNotification Schehduling issue

I have Scheduled my local notification to fire at 4.00 pm and want that notification daily at 4.00 pm but the notification fires before time at 3.44 pm , 3.47 pm etc.
here is the code -
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:self.datePicker.date];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self.datePicker.date];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
[dateComponents setSecond:0.0];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = [NSString stringWithFormat:#"How are you feeling?"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = [calendar dateFromComponents:dateComponents];
localNotification.repeatInterval = kCFCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Try this:
NSDate *myDate = [NSDate date]; // Set your time here
myDate = [myDate dateByAddingTimeInterval:1*24*60*60];
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = myDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = [NSString stringWithFormat:#"How are you feeling?"];
notif.alertBody = #"Reminder is set";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Hope it will help you.

UILocalNotification set repeatCalendar

I am New to NSCalander, NSdates, and NSDateComponents
Basically I have a Local Notification and I want to repeat the fire date based on the selection of the user, let's say on Sunday and Monday only.
We should use repeatCalendar property for the UILocalNotification but I couldn't reach how to setup it.
So any One can help me with simple line of codes ?
Thanks
There is a code snippet to set the UILocalNotification to fire at every sunday 20:00.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit| NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"New updates added for that week!"] forKey:#"new"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(#"notification: %#",notification);
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
enjoy

Resources