Local notification scheduling issue - iphone

I've set up a local Notification and my problem is that I've set it to saturday and with the repeatinterval of one week. However, I set the correct date up but I still get it every sunday but on the correctly set time. Does anybody see my mistake? Oh and dont to forget, if I set the correct day if its ...wednesday I get immediately a notifi after one minute. Don't know where's my fault.
- (void)applicationDidEnterBackground:(UIApplication *)applicatio
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate =
[calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
//set day (saturday)
[componentsForReferenceDate setDay:1] ;
[componentsForReferenceDate setMonth:12] ;
[componentsForReferenceDate setYear:2012] ;
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
// set components for time 18:30.
NSDateComponents *componentsForFireDate =
[calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];
[componentsForFireDate setHour: 18] ;
[componentsForFireDate setMinute:38] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"You are missed!"] ;
notification.alertAction = #"Back";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"Some information"] forKey:#"information"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName = #"Appnotifisound.wav";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
Thanks.

This is because you are creating two different NSDateComponents. One to set the Date, Month and Year, and a different one to set the Hours, Mins and Secs.
This should work :
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];
//set day (saturday)
[componentsForReferenceDate setDay:7] ;
[componentsForReferenceDate setMonth:12] ;
[componentsForReferenceDate setYear:2012] ;
// set time (08:30 PM)
[componentsForReferenceDate setHour: 20] ;
[componentsForReferenceDate setMinute:30] ;
[componentsForReferenceDate setSecond:00] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: #"You are missed!"] ;
notification.alertAction = #"Back";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"Some information"] forKey:#"information"];
notification.repeatInterval= NSWeekCalendarUnit ;
notification.soundName = #"Appnotifisound.wav";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
}

Related

Local Notification issue iphone

I want to make local notification that notify user 5 times a day and repeat them daily,
I have them in a mutable array which object is "hh:mm" which hours and minutes are fixed for GMT+3 town, so I get the current date and find the interval then create a date for the notification
that's the method I implement.
-first applying time zone,
-second if the time before current time so make it for the next day.
-third set local notification for that date.
plz help me
piece of code
with the use of this sample code i have schedule the 2 notification one at Morning 7AM and one at Evening 6PM and repeated it daily and it works superfine hope you can find out your solution with the use of this.
#pragma mark
#pragma mark - Notification Setup
-(void)clearNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
-(void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSMutableArray *arrTemp = [APPDELEGATE.userDefaults valueForKey:#"ParsingResponse"];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:7];
[components setMinute:0];
NSDate *today7am = [calendar dateFromComponents:components];
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = today7am;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatCalendar = [NSCalendar currentCalendar];
notif.alertBody = [[arrTemp objectAtIndex:0] objectForKey:#"Noti_Morning"];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Morning", #"key", nil];
notif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components2 setHour:18];
[components2 setMinute:0];
NSDate *today6pm = [calendar2 dateFromComponents:components2];
UILocalNotification *notif2 = [[cls alloc] init];
notif2.fireDate = today6pm;
notif2.timeZone = [NSTimeZone defaultTimeZone];
notif2.repeatCalendar = [NSCalendar currentCalendar];
notif2.alertBody = [[arrTemp objectAtIndex:0] objectForKey:#"Noti_Evening"];
notif2.alertAction = #"Show me";
notif2.soundName = UILocalNotificationDefaultSoundName;
notif2.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict2 = [NSDictionary dictionaryWithObjectsAndKeys:#"Evening", #"key", nil];
notif2.userInfo = infoDict2;
[[UIApplication sharedApplication] scheduleLocalNotification:notif2];
[notif2 release];
}
}

Local Notification "Everyday at 7:00am" not notifying

I want a notification to go off everyday at 7:00, but it will not go off. I also want it to show in lock screen. Here is all the code I have so far.
-(void)EveryDayNotify
{
NSLog(#"Good Morning Working");
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setAlertBody:#"Good Morning! Have a great day!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatInterval = NSDayCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:07];
[components setMinute:0];
localNotification.fireDate = [calendar dateFromComponents:components];
[localNotification release];
}
Try to use this:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:9];
[componentsForReferenceDate setMonth:11];
[componentsForReferenceDate setYear:2012];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
// set components for time 7:00 a.m.
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
[componentsForFireDate setHour:7];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = [NSString stringWithFormat: #"Good Morning! Have a great day!"];
notification.alertAction = #"go back";
notification.userInfo= #{#"information": [NSString stringWithFormat:#"Some information"]};
notification.repeatInterval= NSCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Thumbs up if this helped! :D
You need to call
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
before you release localNotification. Your code does all the setup but doesn't actually schedule it, which is why you never get it.
even though this is a very old question i just came here from google and yeah isn't it funny how everyone answers by just copying other peoples answer?
So for all the people like me coming from google, have a laugh at the spelling mistake that mite cost you a minute or two of wondering why it doesn't work.
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
works, but
[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
Notiication doesn't...
it's working
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[componentsForReferenceDate setDay:27];
[componentsForReferenceDate setMonth:10];
[componentsForReferenceDate setYear:2016];
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
// set components for time 7:00 a.m.
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
[componentsForFireDate setHour:8];
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = [NSString stringWithFormat: #"БЛАГОСЛОВИТЕ ПРОРОКА (с.а.в)"];
notification.repeatInterval= NSCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
return YES;
}

Issue with canceling UILocaleNotification

I have created a locale notification for my application which fires every day , also in apple developer documentation mentioned that you can fire only 64 notifications , so my question is how can I prevent this limitation ? I mean my notification scheduled that fires everyday per a year , so is this right way to cancel notification and then fires again with scheduled plan ?
- (void)cancelLocalNotification:(UILocalNotification *)notification {
[[UIApplication sharedApplication] cancelLocalNotification:notification;
}
here is my Notification code :
- (void) notification {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSPersianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: now];
[componentsForFireDate year];
[componentsForFireDate month];
[componentsForFireDate day];
[componentsForFireDate setHour:1];
[componentsForFireDate setMinute:2];
[componentsForFireDate setSecond:1];
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.repeatInterval= NSDayCalendarUnit;
notification.alertAction = #"View";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Read your problem here. it surely solved ur problem Repeating an iOS local notification
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [eventText text];
localNotif.alertAction = #"View";
localNotif.soundName = audios;
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

Issue with daily UILocaleNotification

I have created a notification system which push notification everyday at 12:02 AM but I don't know my code works and push notifications from app just for one or two days !! would you please help me to find the problem ?
- (void) LocalNotificationManager {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSPersianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: now];
[componentsForFireDate year];
[componentsForFireDate month];
[componentsForFireDate day];
[componentsForFireDate setHour:00];
[componentsForFireDate setMinute:2];
[componentsForFireDate second];
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDateOfNotification;
notification.timeZone = [NSTimeZone localTimeZone];
notification.repeatInterval= NSYearCalendarUnit;
NSString *date = [self showPersianFullDate];
notification.alertBody = [NSString stringWithFormat:#"today is : %# \n event : %# \n" ,date , events.text];
[self createEvents];
[self retriveData];
notification.alertAction = #"View";
notification.soundName = #"webcam_on.aif";
NSUserDefaults *notificationDefaults = [NSUserDefaults standardUserDefaults];
BOOL DAILYNOTIF = [notificationDefaults boolForKey:#"dn"];
if (DAILYNOTIF == YES) {
NSLog(#"DN is Active");
[[UIApplication sharedApplication] scheduleLocalNotification:notification];}
}
If you want to create a daily notification why have you set the interval to yearly?
Try changing this
notification.repeatInterval= NSYearCalendarUnit;
by
notification.repeatInterval= NSDayCalendarUnit;

iOS: Date Components Inquiry

I was wondering how I would edit the dateComps setDay: to go off 3 days before the date set in the picker. I typed in -3, but that doesn't seem to do the trick.
Any help is much appreciated! Here is my code:
- (IBAction)scheduleNotifButton:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:13];
[dateComps setMinute:30];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in 3 days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
When you say you have typed -3, do you mean you used this line?
[dateComps setDay:[dateComponents day] - 3];
This should in fact work.
Also, you can tell the system to fire any next year by just settings the current year to the date components. You can find the current year just the way you found the date components for pickerDate.
After that, you can set the repeatInterval of the local notification to NSYearCalendarUnit, that way it will fire each year.
Here is a code to subtract 3 days from Today(NOW). You can adapt it for your needs:
NSDate *today = [NSDate date];
NSLog(#"NOW:%#",today);
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComps = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
todayComps.day -=3;
NSDate *dateMinus3Days = [gregorian dateFromComponents:todayComps];
NSLog(#"3 days before NOW:%#",dateMinus3Days);
Your making it too complicated. DateComponents can be modified and then set to a new date.
I think all you need to do is subtract 3 days from the date returned by the date picker, no?
NSDate *pickerDate = [self.datePicker date]
pickerDate = [ pickerDate dateByAddingTimeInterval:-3 * 24 * 60 * 60 ] ; // add this line
Version using date components:
- (IBAction)scheduleNotifButton:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
{
NSDateComponent * deltaComponents = [ [ NSDateComponents alloc ] init ];
deltaComponents.days = -3 ;
pickerDate = [ pickerDate dateByAddingComponents:deltaComponents ] ;
}
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:pickerDate];
[dateComps setHour:13];
[dateComps setMinute:30];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in 3 days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Use dateByAddingComponents:toDate:options:
// ...
NSDateComponents *offset = [NSDateComponents new];
[offset setDay:(-3)];
NSDate *date3DaysBefore = [calendar dateByAddingComponents:offset toDate:pickerDate options:0];
// ...