Local Notification only firing once? - iphone

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

Related

How to set alert view to default

I have an app where I have to use alert view and I know its not possible to set it, so the notification comes in alert view style. How do I set it so that if the user taps that I can they can receive local notifications , it will be automatically set to alert.
you can simply set UILocalNotification with my bellow custom method..
- (void) scheduleAlarm:(NSString *)PaymentTitle FireDate:(NSDate *)tempFireDate {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = tempFireDate; //Set yourDate here
// 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]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
// [dateComps release];//comment it if use ARC
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody =#"Write your Message";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"iClientManagement" forKey:#"App"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
// [localNotif release];//cxomment it if use ARC
}
See My this method on My Blog
Also see another whole demo from this bellow link for display UILocalNotifications
iphone-programming-tutorial-local-notifications

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.

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

UIDatePicker date picking problem

how to show start date and end date in date picker and select them to perform some event in iPhone
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
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]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
You could create a UITableView with two rows, one for the start and one for the end date. Then in the didSelectRowAtIndexPath you set the datePicker.tag to the indexPath.row. When reveicing UIControlEventValueChanged you can simpy get the NSDate from the UIDatePicker and assign it to your variable.

iphone how to get timezone?

i want to get timezone but cant.
i use NSLog is show to me is show.time = 2011-06-29 04:20:00 +0000
here is my code.
-(IBAction)Save:(id)sender{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
// Get the current date
NSDate *pickerDate = [self.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]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
//[dateComps setSecond:[timeComponents second]];
timedata = [calendar dateFromComponents:dateComps];
[dateComps release];
DataClass *obj=[DataClass getInstance];
obj.showdate = timedata;
//NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//[defaults setObject:timedata forKey:#"selectdate"];
//NSLog(#"save time = %#",obj.showdate);
[self dismissModalViewControllerAnimated:YES];
}
You're setting the time zone for your NSDateComponents object to GMT:
[dateComps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Thus your date will be interpreted as being in GMT. If you want the time zone that the device is in, use systemTimeZone:
[dateComps setTimeZone:[NSTimeZone systemTimeZone]];
In general, as noted in the docs for timeZoneWithAbbreviation:, the abbreviations can be ambiguous. If you want to get a particular time zone in a way that will always work no matter the location of the device, you can use timeZoneForSecondsFromGMT:.
You can also set a time zone for your app, if you want, rather than always referring to a specific time zone when creating date-related objects:
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:60*8]];
NSTimeZone * appTZ = [NSTimeZone defaultTimeZone];