scheduledLocalNotifications showing wrong timezone - ios

i can get list of my scheduledLocalNotifications in my table view with this code perfectly ;
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
but the only problem is , my local notifications are firing on correct time but showing with wrong time zone on the list.
I'm also using systemTimeZone for scheduling my local notification like this ;
notification.timeZone = [NSTimeZone systemTimeZone];
What is the correct way to get a list of scheduledLocalNotifications with the correct time zone?
Thanks in advance

I've had the same problem when after i had already scheduled a notification at 3:30 pm and it was correctly scheduled when i NSLog it's fireDate it'll show up in a different time zone and that's how i fixed it.
[localNotification.fireDate descriptionWithLocale:NSGregorianCalendar]
this will return fire date with the Gregorian local (or you should use whatever local you have used when scheduling your local notification)

Related

How to store time and date in plist and compare it with now time iOS 7

I'm trying to develop am iPhone application doing the following tasks:
user stores his movies time and date in plist .
before the movie starts, e.g. 30 minutes before start, the application will give him notification
Now I'm asking for two points:
there is only date field on plist how we can store full time and date at once
how we can keep application compare the current time with movies time on plist even in background
if possible tell me with small example . ( to keep application comparing in background )
example :
Movies.plist
- id
- movie_name
- showing_time
if i have record
id : 1
movie_name: Games B 2
showing_time : 2014-05-22 2:00AM
today is 21-5
i want the application send notification at : tomorrow at 1:00 AM
there is no problem with notification code i know it's like this
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
localNotification.alertBody = msg;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
the problem is how the application will send the notification in the specific time and date
for your information this things is not only for one record in plist , maybe the user will store 10 movies in different days and time .
another example :
**if i open the application and i go to Save New movies i enter 3 different movies name and showing time
and i click save
all the details will saved in Movies.Plist
and i closed the application . now the application should give me local notification before any of Plist Movies start .
this is whole idea .**
NSDates store the date and the time. For example, the current date and time could be represented as: 2014-05-21 17:19:13 +0000.
As for the background process, it'll be unnecessary. When the user sets the movie time, schedule a notification for 30 minutes before the set time. There won't be any ongoing comparison, the notification will fire when the correct time is hit..
You don't need to store something nor to do some background checks, if you want notify user in specific time - just use UILocalNotification, here's the link to Apple documentation on local notifications UILocalNotification

UILocalNotification delayed by 1 hour

I've not experienced this problem personally, but it seems that for a number of my users, a notification being set for one time is actually being triggered an hour later.
Here's the code I use to generate the notification:
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = date;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Alert time!";
notif.alertAction = #"Wake me";
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Fairly standard. The users who've had the problem are on British time, which has daylight savings. I'm wondering if this is an iOS bug of some kind?
I believe the problem was to do with iOS storing a cache of the timeZone. This feature is kind of confusing, as there are 3 different timeZones you can have (and don't worry if half of this confuses you):
[NSTimeZone defaultTimeZone];
Returns the default time zone for the current application. If no default time zone has been set, this method invokes systemTimeZone and returns the system time zone. The default time zone is the one that the application is running with, which you can change (so you can make the application run as if it were in a different time zone).
[NSTimeZone localTimeZone];
Returns an object that forwards all messages to the default time zone for the current application. The local time zone represents the current state of the default time zone at all times. The local time zone adds a level of indirection, it acts as if it were the current default time zone whenever you invoke a method on it.
[NSTimeZone systemTimeZone];
Returns the time zone currently used by the system. If you get the system time zone, it is cached by the application and does not change if the user subsequently changes the system time zone. The next time you invoke systemTimeZone, you get back the same time zone you originally got. You have to invoke resetSystemTimeZone to clear the cached object.
This whole thing baffled me, personally. But that resetSystemTimeZone method seemed interesting:
If the application has cached the system time zone, this method clears that cached object. If you subsequently invoke systemTimeZone, NSTimeZone will attempt to redetermine the system time zone and a new object will be created and cached.
With the possibility of the user moving between time zones, and when some time zones support daylight savings and some don't, and being in mind that Apple themselves have on-going problems with all of this, it seemed like the logical solution would be to make this as non-breakable as possible.
Non-breakable meant that I used systemTimeZone throughout my entire app, and used resetSystemTimeZone on the line before every mention of it.
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = date;
[NSTimeZone resetSystemTimeZone];
notif.timeZone = [NSTimeZone systemTimeZone];
I haven't had a problem with this so far. Hopefully this will help someone.

Old Local Notifications Firing on iOS

I have a set of UILocalNotifications that I am scheduling. They are firing on time and seem to be working fine. However, on occasion, when a NEW notification fires there are multiple (sometimes only one) OLD notifications that fire along with it.
For example, I schedule a UILocalNotification to take out the trash on Monday at 5pm with no repeat interval. It fires no problem and on time. On Tuesday, I have a UILocalNotification to bring in the trash bins for Tuesday at 5pm, again with no repeat interval. When that one fires, I will see the correct notification for NOW, but also below the current notification will be another notification to take the trash out 1 Day Ago. I have not rescheduled this notification. It appears to be the notification from yesterday.
It is very bizarre and I cannot reproduce it on any sort of consistent basis. I thought that maybe there were some old notifications being added somehow so I added some logic to run through all scheduled notifications and remove any that had a fire date that was in the past but that did not help. Anyone else ever see this problem? Is there some manual clearing of a notification that I need to do when one fires?
EDIT: Added some of the scheduling code
//only schedule if the alert date is later than now
if ([AMDateUtil isNowEarlierThanDate:alertDate]) {
//create the notification and setup some properties
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = alertDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertAction = nil;
localNotification.soundName = UILocalNotificationDefaultSoundName;
//add the local notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
We had the same issue, but managed to fix it by batching our calls to the API. Instead of calling scheduleLocalNotification each time, instead build an array of all notifications you want to be scheduled. Like this:
NSMutableArray *notifications = [[[UIApplication sharedApplication] scheduledLocalNotifications] mutableCopy];
// add notifications to the mutable array
// same goes for removing notifications
[[[UIApplication sharedApplication] setScheduledLocalNotifications:notifications];
Also make sure you are calling it from the main thread.
Our problems seem to have gone away after doing this.
The first thing that I can think of is to check the repeatInterval of your notifications. It seems that you may want your repeatInterval to be weekly, but the interval seems to be set to daily. To set the repeatInterval to weekly use:
localNotification.repeatInterval = NSWeekCalendarUnit;
Perhaps in some places you may be accidentally using
localNotification.repeatInterval = NSWeekdayCalendarUnit;
which repeats once a day.
If that is not the issue, then perhaps if you post some of your code where you schedule the notifications someone could help you. If a notification has a repeat interval of 0 (or not repeating) then you should not have to manually clear it. If the repeat interval is not 0 then you will have to manually clear it to get it to stop repeating.

How to know UILocalNotification remaining time before fires?

I would like to know remaining time before UILocalNotification fires and then show it in some UIabel in real time.
How can I do that?
Notification has property fireDate - The date and time when the system should deliver the notification.
So you can use it and current date for calculation remained time.
Following two methods available to get the Registered LocalNotifications, get the Object of it and access FireDate, and calculate your time
To get all the notifications
[[UIApplication sharedApplication] scheduledLocalNotifications];
To get particular notification object if you know Which object to get.
[[UIApplication sharedApplication] scheduleLocalNotification:yourNOtificationObject];

iOS calendar check in background

I would like my app to check on a date while running in the background. At that point, I would like to do a local notification, but I know how to do that... What I am interested in is, if somebody even clicks the home button, and my app is in the background, I would like my app to check what date it is (once a day, while in the background), and if it is a particular date, I would like to do a local notification - a sort of an app-specific calendar...
Any ideas??
Your app (probably) doesn't run in the background so you cannot check the date, but you can schedule a local notification for a specific date (fireDate) with the following
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*90]; // the date you want the notification to fire.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
For more info see Apple's docs
Maybe this post would be helpful for you http://mobileorchard.com/ios-advanced-programming-understanding-ios-4-multitasking/ . Good luck!

Resources