I am trying to get my app to fire a notification based off a scheduled event. The date given is from a schedule that will take place in EST. The time given is Fri Feb 21 2014 12:00:00 -0500 (Noon EST).
When the button to schedule a notification is pressed, it runs this code:
NSString *message = [#"15 minutes until " stringByAppendingString:self.thetitle];
NSLog(#"original%#", self.thedate);
NSDate *newDate = [self.thedate dateByAddingTimeInterval:-60*15];
NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
// [formatter3 setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter3 setTimeStyle:NSDateFormatterShortStyle];
[formatter3 setDateStyle:NSDateFormatterShortStyle];
NSString *detailstext = [formatter3 stringFromDate:newDate];
NSDate *othernewdate = [formatter3 dateFromString:detailstext];
NSLog(#"other%#", othernewdate);
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = othernewdate;
notification.alertBody = message;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.hasAction = YES;
notification.alertAction = NSLocalizedString(#"View", #"View notification button");
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Since the time being set on the notification fireDate is the othernewdate, I ran an NSLog in the console to see what it showed. It came back:
2014-02-21 16:45:00 +0000
This seems to be perfect, because that time in EST would be 11:45 AM, or 15 minutes before noon, which is what I wanted.
The issue is this:
I am testing this in CST. I run the app, tell it to schedule a notification for this event, and then change my phone clock to be EST and it ends up firing the notification at 10:45.
There will be people using this app from different time zones, and then traveling to EST for when the conference that this app is for starts. I don't want them to schedule lots of notifications while in a different time zone and have it fire at the wrong time. Any suggestions?
If you want the notification to fire at an absolute date/time, independent of the time zone, then you must set notification.timezone = nil.
From the documentation:
"The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock."
Related
I'm displaying a local notification every day at 9AM with exchange rates. But if value of (let's say euro for example) is the same as yesterday then it should't fire. I'm downloading data from my server using XML. So my question is it possible to parse XML data(every day at 9AM before displaying notification) while app is not running (killed with task manager), since displaying notifications is (even when app is killed totally).
This is how I fire my notification:
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:9];
// Gives us today's date but at 9am
NSDate *next9am = [calendar dateFromComponents:components];
if ([next9am timeIntervalSinceNow] < 0) {
// If today's 9am already occurred, add 24hours to get to tomorrow's
next9am = [next9am dateByAddingTimeInterval:60*60*24];
}
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = next9am;
notification.alertBody = #"Euro value: <PARSED VALUE HERE>";
// Set a repeat interval to daily
notification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
If you can send APNS push notifications to your app you can wake it from background and have it process your xml (when exchange rates change for example) and schedule the local notification.
You have to use the silent push notification. It will launch you application in background without knowing the user and then you can download the xml and do the comparison. if it is same fire the local notification which will shown to the user and then user can proceed further.
NOTE: Apple gives max 30 seconds to perform you task in background
I'm working on an application in which i want to show a UILocalNotification for an inconsistent interval. notification will appear on the day of week user want to see i.e. user selects monday, tuesday and friday then notification will be visible on these days of each upcoming week.
Thanks for any help in advance.
I think what you need to do is schedule multiple UILocalNotification instances for the irregular periods in your interval. You can use the repeatInterval property to have each UILocalNotification repeat.
For your example, you could have three UILocalNotification instances scheduled for Monday, Tuesday, and Friday, and then set the repeatInterval value of each of these instances to NSWeekCalendarUnit.
A UILocalNotification takes an NSDate for its fireDate property. Why don't you create an NSDate instance based on feedback from the user through the UI and use that?
To create the NSDate object, you could either simply call initWithTimeIntervalsSinceNow: using a multiple of seconds. For example, to create an NSDate representing the time 6 days and 5 hours from now:
NSDate* futureDate = [[NSDate alloc] initWithTimeIntervalSinceNow:6*24*60 + 5*60];
Or, if you need to take the user's particular calendar (remember there are many different calendars in use around the world) or timezone into account, you should also check out the NSCalendar and NSTimeZone classes. The 'Date and Time Programming Guide' is a great walkthrough of these.
Put On AppDelegate.m and call from enterbackground and terminal method
-(void)localNotification{
UILocalNotification *scheduledAlert;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
scheduledAlert = [[[UILocalNotification alloc] init] autorelease];
NSDate *date = [NSDate date];
NSLog(#"%#",date);
scheduledAlert.fireDate = [NSDate dateWithTimeIntervalSinceNow:86400];
scheduledAlert.timeZone = [NSTimeZone defaultTimeZone];
scheduledAlert.repeatInterval = NSDayCalendarUnit;
NSArray *arr =[[NSArray alloc]initWithObjects:#"Your Notification 1",#"Your Notification 2",#"Your Notification 3",nil];
int jk = arc4random()%arr.count;
scheduledAlert.alertBody = [arr objectAtIndex:jk];
[[UIApplication sharedApplication] scheduleLocalNotification:scheduledAlert];
}
I have a date from date picker in ios, I get the date and the time and I convert this to string and saved in localStorage. Then, I get this string date and convert it to NSDate and use it in my notification. But my problem is that the notification are not run because the date changes after conversion.
The time and date from picker is: dateFromPicker=Thursday-20-March-2014 09:21 AM
The time and date from picker before 4 hours is :StringDateBefore4hours=Thursday-20-March-2014 05:21 AM
The string time before 4 hours after converting to NSDate is :DateBefore4hours=2014-03-20 00:21:00 +0000
The time is changed and my notification not run on the time selected what can we do help me please please .
That is code for getting date from picker and assigning to notification
NSDate *date=[self.DatePicker date];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.alertBody = self.TextField.text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber=[[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Hello i am facing a weird kind problem. Actually i want to schedule a daily notification (only once a day) at 8:00 AM. Below is my code for scheduling daily notification.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"08:00"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody = #"You just received a local notification";
localNotification.alertAction = #"View Details";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[formatter release];
[date release];
My problem is, I receive 2 local notifications. One at 8:00 AM and other at 10:00 AM. Why i am getting notification at 10:00 AM. I am scheduling it at 8:00 AM only. I know UILocalNotification library have some other weird kind of problems/bugs on most of apple devices. I just want to confirm whether there is some mistake in my code or it is a weird behaviour of UILocalNotification Library. I dont know why Apple is not working on resolving the issues being reported by many developers about UILocalNotification.
Note: I am using Xcode 4.6 and iOS 6.1
This would be the BEST link to get your answer.
I have a method that makes the store of a UILocalNotification:
- (void)save:(NSString *)program at:(NSDate*)date {
NSLog(#"date to save: %#", date);
// NSLog(#"timezone: %#",[date descriptionWithLocale:[NSLocale systemLocale]]);
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
NSLog(#"date to fire: %#", date);
localNotification.fireDate = date;
NSString *s=[program stringByAppendingString:#" is now on "];
NSString *title=[s stringByAppendingString:channel];
localNotification.alertBody = title;
localNotification.alertAction = #"Show...";
//localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
NSLog(#"notification to save %#",localNotification);
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
And I have as output:
date to save: 2013-08-29 17:00:00 +0000
date to fire: 2013-08-29 17:00:00 +0000
notification to save <UIConcreteLocalNotification: 0xc0c4240>{fire date = Thursday, August 29, 2013, 6:00:00 PM Central European Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Thursday, August 29, 2013, 6:00:00 PM Central European Standard Time, user info = (null)}
Despite uncommenting the timezone setting, the UILocalNotification is always incrementing by one hour, why, and how ?
Thank you for helping.
The date you pass in in GMT, which in you case is does not match your local time zone.
So when you set the date to 17:00 your time corrects it to you timezone (CET) which is GMT+1.
Thus an hour gets added to you date.
A solution is to set the UILocalNotification timezone to GMT:
localNotification.timeZone = [NSTimeZone timeZoneWithName#"GMT"];
From the Apple documentation:
The date specified in fireDate is interpreted according to the value
of this property. If you specify nil (the default), the fire date is
interpreted as an absolute GMT time, which is suitable for cases such
as countdown timers. If you assign a valid NSTimeZone object to this
property, the fire date is interpreted as a wall-clock time that is
automatically adjusted when there are changes in time zones; an
example suitable for this case is an an alarm clock.