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.
Related
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
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];
}
}
}
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.
I'm creating a reminder app.In that a local notification is firing at particular time.I'm also implementineg a function for switching ON/OFF the notification.Notification is working fine but cancel function is not working.I'm using the below code.Is there any thing wrong in this code.Please help me.Thanks in advance.
-(void)swit
{
if (flag==1) {
//if (toggleSwitch.on) {
toggleSwitch.on=YES;
flag=1;
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// 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 setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
NSLog(#"%#",itemDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"Tip of the day";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"it is working");
}
else {
toggleSwitch.on=NO;
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
NSLog(#"it is not working");
}
}
The reason why the notification gets never cancelled if due to your flag var :
// so if flag == 1
if (flag==1) {
toggleSwitch.on=YES;
// the value doesn't change
// you will never reach the else part
flag=1;
When you detect flag==1, assign it another value, so the next time your notification will be cancelled. Also don't forget in the else part, to put flag back to 1.
In my i'm using a action sheet picker view. It is working fine. But I want to fire a local notification when a particular time is set in time picker, it is not firing the local notification. I used this code:
enter code here
- (void)actionPickerDone {
if (nil != self.data) {
//send data picker message[self.target performSelector:self.action withObject:[NSNumbernumberWithInt:self.selectedIndex]];
}
else {
//send date picker message
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// 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 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];
NSLog(#"i8i8i88i");
// Notification details
//localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
Is there any thing wrong in this code.please help me.
I executed your code and the notification didnt fire. Then I uncommented the localNotif.alertBody assignment statement and it did fire. Local notification will not appear as an alert if you do not specify an alertBody. Plus the point raised by Idan is also valid. If your app is in foreground the notif wont be displayed rather you will get a callback in your app delegate in which you should display an alert with the message notification.alertBody for consistency.
Hope this helps :)
PS you dont need all the calender components thing youre doing. Log both of them and youll see that both pickerDate and itemDate are same.