Issue with canceling UILocaleNotification - iphone

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];

Related

How to cancel a UILocalNotification based on time

I'm writing an application based on UILocalNotifications. I having the user input a time on a date picker and that time will represent when the last UILocalNotification will fire. After the user hits a UIButton UILocalNotifications will fire based on a time interval until the time they selected occurs, then the app will stop.
I schedule the maximum number of UILocalNotifications before the user inputs a time. Is there a way to cancel the UILocalNotifications that are scheduled after the user's input time, based on time? Or is there a way to cancel all scheduled notifications after the inputted notification fires? The code is setting the last timer and the first timer based of the input time. Thanks for the help.
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:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
/**************Setting the final alarm*********************/
UILocalNotification *finalAlarm = [[UILocalNotification alloc] init];
if (finalAlarm) {
finalAlarm.fireDate = itemDate;
finalAlarm.timeZone = [NSTimeZone defaultTimeZone];
finalAlarm.repeatInterval = 0;
finalAlarm.soundName = UILocalNotificationDefaultSoundName;
finalAlarm.alertBody = #"Test message...";
[[UIApplication sharedApplication] scheduleLocalNotification:finalAlarm];
}
//Formatting original date in the date picker for readability
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH"];
NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];
//isolate current date/time
NSDate *currentDate = [NSDate date];
//formatting the current date for readability
[dateFormatter setDateFormat:#"HH"];
NSString *currentForamttedDate = [dateFormatter stringFromDate:currentDate];
//current hour
int currentHour = [currentForamttedDate intValue];
/**************Setting the first alarm*********************/
//current hour plus 2
int firstAlarmHour = currentHour + 2;
//creating the first alarm
NSDateComponents *firstAlarm = [[NSDateComponents alloc]init];
[firstAlarm setDay:[dateComponents day]];
[firstAlarm setMonth:[dateComponents month]];
[firstAlarm setYear:[dateComponents year]];
[firstAlarm setHour:firstAlarmHour];
[firstAlarm setMinute:[dateComponents minute]];
[firstAlarm setSecond:0];
//creating a date from the components
NSDate *firstAlarmDate = [calendar dateFromComponents:firstAlarm];
//setting the first alarm
[self scheduleNotificationForDate: firstAlarmDate];
You can use the following code to get all the notifications and then cancel them based on your specifications.
if ([[UIApplication sharedApplication].scheduledLocalNotifications count] > 1){
for (int i = 0; i < [[UIApplication sharedApplication].scheduledLocalNotifications count]; i++){
UILocalNotification * notification = [[UIApplication sharedApplication].scheduledLocalNotifications objectAtIndex:i];
if (/* if after your fire date */){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}

Local Notification only firing once?

I have a local notification set to fire on the specified date when a user changes the date
on a UIDatePicker. However, when I ran the app, the notification fired correctly, but when
I went and made a small change, then ran it again, the notification didn't fire. Here's my
code, please help!
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [datepicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// 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]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"Timer";
// Set the action button
localNotif.alertAction = #"View";

How to get a date and time from a UIDatePicker and notify the user when that date has arrived? [duplicate]

This question already has an answer here:
Register for Local Notification
(1 answer)
Closed 9 years ago.
I need to get a date and time from a UIDatePicker and fire a local notification when that date and time has arrived. How would I go about doing this? Thanks!
use this code
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = your date from date picker;
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// 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]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"Write your text here";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Specify custom data for the notification
NSStream *str=[NSString stringWithFormat:#"%d",cId];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:str forKey:#"NotificationDate"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"The notifications are:%#",[[UIApplication sharedApplication]scheduledLocalNotifications]);
NSLog(#"The %#",localNotif.userInfo);
[localNotif release];
Use self.datePicker.date to store the target date with time.
Then make a NSTimer that will poll every miilisecond to check if current date equals to target date. As both become equal then show the alertview.

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];
// ...

local notification not firing in iphone

This may be a repeated question,but please help me.I used the code below for firing a local notifification,but it is not firing.I used a picker to set the time for firing the local notification.Please help me..
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
if (nil != self.data) {
//send data picker message
[self.target performSelector:self.action withObject:[NSNumber numberWithInt:self.selectedIndex]];
}
else {
//NSDate *date = [NSDate date];
//NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
//[dateFormat setDateFormat:#"HH:mm:ss zzz"];
//NSString *dateString = [dateFormat stringFromDate:date];
//send date picker message
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePickerView date];
//NSLog(#"date ==%#",pickerdate);
//Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// 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]];
//NSLog(#"day%#",[dateComponents day]);
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
NSLog(#"%#what is this",itemDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
//Notification details
localNotif.alertBody = #"Tip of the day";
//Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[localNotif release];
}
Where is the code for scheduling the local notification?
You have to make this call somewhere before you release your UILocalNotification object:
- (void)scheduleLocalNotification:(UILocalNotification *)notification
Like so,
[[UIApplication sharedApplication] scheduleLocalNotification: localNotif];
Its only after you call this method that your notification gets copied, and hence safe for releasing the notification object.