iOS calendar check in background - ios

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!

Related

IOS/Objective-C: Is there a way to keep track of notifications before and after they are fired?

I gather that IOS does not give you access to the notifications center in order to keep track of notitifications.
However, many apps do seem to keep track of notifications so that when you see the badge on your home screen or a notification fires and you tap inside the app you can view all the notifications. Linkedin does this for example.
In my case, all the notifications refer to managedobjects in one entity in core data so I tried to hack this by estimating which notifications should have fired given their timestamps in core data. However, this is proving to be an unreliable approach. A fetch that grabbed every item beginning with the first notification since last opened would have to know the time stamp of the first notification since last opened. However, while you know the current time, you don't really know the time of the first notification if you don't know what the first notification since the last opening of the app is.
Has anyone devised a way or know of a library to accurately track notifications? It occurred to me one way might be create an accessible doppelganger of the notificationcenter in core data or an array and keep the notifications there. But that's as far as I've gotten.
To create the cache of future notifications in the notification center, I just fetch anything with a date in the future from core data and add the notification to the notification center:
NSDate *now [NSDate* date];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"event!=nil AND event.length>=1&&starttime>=now"];
//perform fetch code here
NSInteger resultCount = [fetchedObjects count];
UILocalNotification *reminderNotification;
for (int i = 0; i < resultCount; i++) {
reminderNotification = [[UILocalNotification alloc] init];
reminderNotification.fireDate = event.starttime;
reminderNotification.timeZone = [NSTimeZone defaultTimeZone];
reminderNotification.alertAction = #"View Event";
reminderNotification.alertBody = event.event;
reminderNotification.applicationIconBadgeNumber = resultCount+1;
//schedule the notification!
[[UIApplication sharedApplication]
scheduleLocalNotification:reminderNotification];
resultCount++;
}
Thanks in advance for any suggestions.

How can I Specify Relative Time in UILocalNotification Alert Body (i.e. "starting in X minutes")?

I'm creating a local notification like this:
NSDate *fireDate = // start date from model
NSString *startingInMinutes = #"5 minutes"; // also actually from model
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:#"Your event is starting in %#", startingInMinutes];
notification.fireDate = fireData;
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
After the notification is shown to the user, however, the "5 minutes" string is quickly outdated.
For example, if the user doesn't interact with the notification for 30 minutes, it still shows "Your event is starting in 5 minutes." This is obviously due to the fact that the "5 minutes" is a hard-coded string.
Is there any syntax to specify a relative time (i.e. "in X minutes") in the alert body of a UILocalNotification?
You can't do anything about that.
It's a static text.
See documentation
This isn't possible because the notification's payload is set at the time it is scheduled (UILocationNotification) or delivered to APNS (remove notification).
You have two choices:
Leave your message as is and hope that the user realizes that the content of the message reflects a frozen point in time and can mentally do the math using the notification's timestamp.
Adjust the text to be slightly ambiguous ("Your event is starting soon") or reference an absolute time ("Your event starts at 4:00pm").
WatchKit does include WKInterfaceTimer, which is an OS-controlled counter label. It was created to decrease the amount of communication needed between an Apple Watch and an iPhone just to update UI as the time changes. If this functionality is important to you in a notification context, I would encourage you to submit a bug report asking for the enhancement at https://bugreport.apple.com.

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

scheduledLocalNotifications showing wrong timezone

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)

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.

Resources