Only let local notification appear once - iphone

I have a huge problem! My problem is, that I've implementet a localnotification code in my delegate. I've put in this code in didEnterBackround method. However, I get a localnotifi for each didEnterBackround. Is there a possible way that only a new notification firedate starts, if the latest was gone? Like if I have a localnotifi which fires after 5 days. When I open and close the app 5 times and wait 5 days I get 5 localnotifications. Is it possible to let the app make only one firedate and after this appears a new one gets startet?!
My code:
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentDate];
NSDateComponents *timeComponents = [calender components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:currentDate];
NSDateComponents *temp = [[NSDateComponents alloc]init];
[temp setYear:[dateComponents year]];
[temp setMonth:[dateComponents month]];
[temp setDay:[dateComponents day]];
[temp setHour:[timeComponents hour]];
[temp setMinute:[timeComponents minute]];
[temp setSecond:[timeComponents second] +10];
NSDate *fireTime = [calender dateFromComponents:temp];
UILocalNotification *localN = [[UILocalNotification alloc]init];
localN.fireDate = fireTime;
localN.timeZone = [NSTimeZone defaultTimeZone];
localN.alertBody = #"BLA BLA BLA!";
localN.alertAction = #"GO BACK";
localN.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localN];

You're able to cancel all scheduled notifications:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you don't want to remove ALL scheduled notifications (perhaps you have something else scheduled), then take a look at cancelLocalNotification: which allows you to cancel only a single instance.

Related

iPhone Alarm App mysterious inactivation (UILocalNotification)

I am in the final stages of development for an iPhone Alarm App
The alarm works through notification centre.
Work well for short time delays (say 2 hours)
Does not work in the morning (Long time delay 8 hours)
Also it seems to work fine on phones that do not have internet access?
Could there be some kind of automatic time updating/syncing that is disturbing alarm function?
Is the 8 hour+ time delayed neutralising the notificaiton?
Does the app need to be in the foreground?
So the alarm works for short time delays, but not in the morning (When it is meant to!)
We are due to submit update to Apple very soon!
Thank you Kindly,
Henry (Aus.)
-(void)addLocalNotification:(myAlarmData *)ma WeekDay:(int)mweekday
{
UILocalNotification *noti = [[UILocalNotification alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *dcom = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:now];
[dcom setWeekday:mweekday];
[dcom setHour:ma.mHour];
[dcom setMinute:ma.mMinute];
[dcom setSecond:0];
NSDate *fireDate = [gregorian dateFromComponents:dcom];
noti.fireDate = fireDate;
noti.timeZone = [NSTimeZone localTimeZone];
noti.alertBody = [NSString stringWithFormat:#"Wake up %#!", [GlobalData gSettings].name];
noti.soundName = [NSString stringWithFormat:#"%#.caf", ma.soundName];
noti.alertAction = #"OK";
noti.repeatInterval = NSWeekCalendarUnit;
noti.userInfo = [[[NSDictionary alloc] initWithObjectsAndKeys:
ma.mid, #"mid",
[NSString stringWithFormat:#"%d", mweekday], #"day",
ma.soundName, #"sound",
[NSString stringWithFormat:#"%d", ma.snooze], #"snooze",
ma.title, #"title",
#"Close", #"action",
#"real", #"more",
nil] autorelease];
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
[noti release];
[gregorian release];
}

iOS notification override Silent mode - iPhone Alarm Clock App

I am working on an alarm clock App
The alert + music channels through the notification centre
It will not activate if the iPhone is set to silent mode
Many users have requested that the alarm overrides silent mode
Is this possible?
Cheers!
(void)addLocalNotification:(myAlarmData *)ma WeekDay:(int)mweekday
{
UILocalNotification *noti = [[UILocalNotification alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *dcom = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:now];
[dcom setWeekday:mweekday];
[dcom setHour:ma.mHour];
[dcom setMinute:ma.mMinute];
[dcom setSecond:0];
NSDate *fireDate = [gregorian dateFromComponents:dcom];
noti.fireDate = fireDate;
noti.timeZone = [NSTimeZone localTimeZone];
noti.alertBody = [NSString stringWithFormat:#"Wake up %#!", [GlobalData gSettings].name];
noti.soundName = [NSString stringWithFormat:#"%#.caf", ma.soundName];
noti.alertAction = #"OK";
noti.repeatInterval = NSWeekCalendarUnit;
noti.userInfo = [[[NSDictionary alloc] initWithObjectsAndKeys:
ma.mid, #"mid",
[NSString stringWithFormat:#"%d", mweekday], #"day",
ma.soundName, #"sound",
[NSString stringWithFormat:#"%d", ma.snooze], #"snooze",
ma.title, #"title",
#"Close", #"action",
#"real", #"more",
nil] autorelease];
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
[noti release];
[gregorian release];
}
We can override silent switch to play sounds.
In this case it depends on how iOS manages to play sounds when a notification is triggered.
Have you checked this on device.?
You should take a look at AudioSession categories.
AVAudioSessionCategoryPlayback should do the trick.

LocalNotifiction for wakeup application

In my application i want to set local notifiaction on a particular time if my application killed from background else if my application in running condition then i dan't want to show localnotification
I have written code for local notification
NSCalendar *calendar1 = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate1 =self.dateEvening;//[self.dateEvening dateByAddingTimeInterval:60*1];// [self.pickerView date];
// Break the date up into components
NSDateComponents *dateComponents1 = [calendar1 components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate1];
NSDateComponents *timeComponents1 = [calendar1 components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate1];
NSDateComponents *dateComps1 = [[NSDateComponents alloc] init];
[dateComps1 setDay:[dateComponents1 day]];
[dateComps1 setMonth:[dateComponents1 month]];
[dateComps1 setYear:[dateComponents1 year]];
[dateComps1 setHour:[timeComponents1 hour]];
[dateComps1 setMinute:[timeComponents1 minute]];
[dateComps1 setSecond:[timeComponents1 second]];
NSDate *itemDate1 = [calendar1 dateFromComponents:dateComps1];
[dateComps1 release];
UILocalNotification *localNotif1 = [[UILocalNotification alloc] init];
if (localNotif1 == nil)
return;
localNotif1.fireDate = itemDate1;
localNotif1.timeZone = [NSTimeZone defaultTimeZone];
localNotif1.alertBody = #"Open application";
localNotif1.alertAction = #"View";
localNotif1.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif1];
[localNotif1 release];
Above code works fine if application will close from background but if my application in running condition then I don't want local notifications to be fired.

set datepicker at a specific time

Hi guys i have a problem
i my code i set a datepicker with the date of today, after that if the user change the picker i set a alarm to ring at the date of picker.
How can i "block the picker" on these date?
here my code, i hope in your help
NSDate *pickerDate = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Divisione della data in fattori
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set Up dell'alarme considerando tutti i fattori
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]];
//Notifica in meno di un minuto
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
[alarmDatePickerUno setDate:itemDate animated:YES];
[alarmDatePickerDue setDate:itemDate animated:YES];
}
// ALARM UNO
// Set Allarme Uno
-(IBAction)setAlarmUno:(id)sender{
NSLog(#"picker in allarme");
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
//Setting dell'allarme in base al picker
AlarmDateUno = [alarmDatePickerUno date];
NSLog(#"allarme settata alle");
NSLog(#"%#", AlarmDateUno);
alarmDatePickerUno.userInteractionEnabled = NO;
alarmDatePickerDue.userInteractionEnabled = NO;
EDIT AFTER CLARIFICATION
If you want to use a date between app launches, you need to persist the date somehow. I would suggest using NSUserDefaults, but you could also go for the more complicated solution of Core Data. Using NSUserDefaults you would check every time you create your UIDatePicker for the saved value and use that to initialize the picker.
// Get the date
NSDate *unoDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"PickerUnoKey"];
if (unoDate) { // If we have a persisted date, we set the date of the datePicker
[alarmDatePickerUno setDate:unoDate animated:NO];
}
Then when the user changes the value of the picker, you need to store the new value.
// When the datePicker value has changed, we store the value
[[NSUserDefaults standardUserDefaults] setObject:alarmDatePickerUno.date forKey:#"PickerUnoKey"];

If/Else statement for Date..iPhone SDK

I have this bit of code here. How would I write an if/else statement to determine whether the itemDate has already passed? So if itemDate has already passed, then localnotif.fireDate should be what it is now, plus 86400 (for 24 hours) else leave as is.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:item.day];
[dateComps setMonth:item.month];
[dateComps setYear:item.year];
[dateComps setHour:item.hour];
[dateComps setMinute:item.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60)] ;
NSLog(#"fireDate is %#",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
You can retrieve the current date using [NSDate date].
NSDate has this really convenient method called laterDate: that compares a date with the receiver and returns which ever one is later.
You can see if two dates are equal using isEqual:
In other words:
NSDate * notificationDate = [localNotif fireDate];
NSDate * now = [NSDate date];
if ([[now laterDate:notificationDate] isEqual:now]) {
//in a comparison between "now" and "notificationDate", "now" is the later date,
//meaning "notificationDate" has already passed
notificationDate = [notificationDate dateByAddingTimeInterval:86400];
}
[localNotif setFireDate:notificationDate];