iOS 8: Using timer in the background - iphone

I am implementing an iOS8 app that runs in the background and sends a notification that asks a question every 10 mins for 3 hours. I have heard that running timers in the background for 3 hours cannot be done because the app will be suspended after a couple of minutes of going the background state. Is that true? If not will the appstore accept such an app?

There is no point for sending local notificatiosn for 3 hours in background.
Best option would be to prepare a logic which will will create a loop and schedule multiple local notification for 3 hours for example you can schedule n notifications for m duration. This way even your app will be suspended you will be getting local notifications and you can unregister all notification if user open the app.
And regarding your second question:
There is no relation with appstore policy for scheduling local notifications in background.
NSDate *newDate = [[NSDate date] dateByAddingTimeInterval:totalDuration];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = newDate;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
This is just a rough code that you can schedule localNotifications by creating a loop with certain timeintervals. Just put your own logic i.e. 10 mins for 3 hours and supply newDate by adding time interval - 10*60secs

Related

How to set UINotification for specific days like every Monday and Friday?

I am writing an alarm app. In the app, user can select days from the picker for setting alarm on that day with time. But I don't know to set alarm (UILocalNotification) for specific day in a week (0 - 7).
I just wanted to set fire date of UILocalNotification on specific day like ex. Monday, Friday and Sunday..
I have searched on net and got many similar forums but none of them worked for me.
Any suggestions ?
Set the local notification fireDate for the first notification that should be made. Then set the repeatInterval for the duration between any notification and the next notification.
It would be more helpful if you have included codes which you have tried.
Anyways,
NSDate myOwnDate; // myOwnDate can be a date that is on a specific day. Make sure it is a valid date variable
UILocalNotification* local_notification = [[UILocalNotification alloc] init];
[local_notification setFireDate:myOwnDate];
[local_notification setRepeatInterval:NSWeekCalendarUnit];
[local_notification setAlertBody: #"Your alarm is ringing!"];
UIApplication* app = [UIApplication sharedApplication];
[app scheduleLocalNotification:local_notification];
Pretty straightforward I would say.

Set a task for a certain date (iOS app)

Assume that I create a task today, and say that I want to be reminded of it (via a push notification) 10 days from today. Essentially, how do I constantly keep track of what day it is (even when my app isn't actively being used) to finally recognize when it becomes 10 days from today?
You don't do this.
You would schedule the notification for when it is supposed to happen like this...
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate someDateInTheFuture];
notification.alertBody = #"This is your alert. Do something";
notification.alertAction = #"Alert";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
The notification will then fire when it gets to the date that you set. You don't have to check for it yourself in the app. It just happens.

How do I increase the value of applicationIconBadgeNumber during app doesn't run?

How do I increase the applicationIconBadgeNumber by localNotification?
Is it possible to increase the applicationIconBadgeNumber to add [ 1 ] every day by localNotification?
If localNotification doesn't work, how can I change applicationIconBadgeNumber?
It was done by AppName:Count it!
http://itunes.apple.com/app/id443809931?mt=8&ign-mpt=uo%3D4
I asked a question by e-mail to the developer of this application seven months ago.
However, a reply did not come.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = startDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval = NSDayCalendarUnit;
//I don't know the follow code.I want to increase BadgeNumber during repeatInterval
localNotif.applicationIconBadgeNumber = 1++;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Local notifications can set the application icon badge number. Obviously, the app has to run to schedule the local notification, but does not need to be running to receive it.
The other option, of course, is to use remote notifications.
You can try to use [UIApplication setKeepAliveTimeout:handler:] to execute a function every X seconds (at least 600). In that function you can increase the number.
The setKeepAliveTimeout require to be a VOIP application, so i'm not sure if apple can approve you anyway.

UILocalNotification in iOS 5

I'm newbie to iOS development. I have a quick question and I couldn't find any info on Apple's Developer Community.
I'm making an app with ability to schedule tasks. I plan on using UILocalNotification to set reminders.
Is that the correct method or are there better ways?
If I set a UILocalNotification, and the user changes the task due date associated with that notification, can I change the UILocalNotification?
iOS 5 has the new notifications pull-down. Does UILocalNotification automatically go in there?
Cheers,
Dean
UIlocalNotification is better option if u know that ur schedule tasks cannot exceed 64(per app) because one app cannot schedule more than 64 notification. Also u can repeat ur tasks only at unit second, minute, hour ,daily, monthly, yearly, weekday [more in apple's developer documentation]. If u want to repeat at ur own custom time then u have to schedule more than 1 notification(e.g. twice daily at 7:00 am and 9:00 pm then two notification at 7:00 am and 9:00 pm with "repeatInterval:NSDayCalendarUnit" )
Yes. U can change .
UILocalNotification *cancelThisNotification = nil;
for (UILocalNotification *someNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
if([[someNotification.userInfo objectForKey:#""] isEqualToString:#"") {
cancelThisNotification = someNotification;
someNotification = nil;
[someNotification release];
// if wanna cancel
[[UIApplication sharedApplication] cancelLocalNotification:cancelThisNotification];
// if wanna change e.g. fire date
cancelLocalNotification.firedate = [NSDate urdate];
}
}
// but it is better to cancel any notification
3.u can(and not if u dont want) by going to Settings => Notification and chenge ur prefernce for ur app

How to show a UILocalNotification when ASINetworkQueue finishes all requests?

I'm using ASIHTTPRequest to download multiple files while the iPhone app is running in the background. I want to present a UILocalNotification when the queue finishes.
The following delegate method isn't called until the app is resumed:
- (void)queueFinished:(ASINetworkQueue *)aQueue
{
NSLog(#"Queue finished");
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = NSLocalizedString(#"All downloads completed");
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
[localNotification release];
}
}
So, how can I make this notification appear?
The reason your delegate isn't getting called is likely because your app is suspended in the background. If you are doing some sort of lengthy network process that continues after the user closes the app, you can use -[UIApplication beginBackgroundTaskWithExpirationHandler:] when you start the network tasks so that your application continues running in the background until you're done with the network tasks. However, it can still expire so you're not guaranteed to get enough time to finish.
From previous SO question notification when program is in background iOS 4
You do realize that when your app is in a suspended state, you won't receive any notifications -- and this is right in the documentation. There are only 3 classes of applications that can receive notifications: Audio applications (like iPod and analogues), location based applications, and voip apps. Your plist has to be set up correctly if your app is one of those applications.
Use this:
UILocalNotification *localNot = [[UILocalNotification alloc] init];
localNot.alertBody = #"Your Text";
localNot.alertAction = #"Name on the button";
localNot.fireDate = [NSDate date];
localNot.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNot];
What you're doing:
Create the LocalNotification
Add the body of your LN
Add the name of the button which appears on the "alert"
Set the fireDate to the actual date and time (maybe you need to increase the actual date with 1 or 2 seconds - for this use: dateByAddingTimeInterval:)
Set the soundName (you could also use a custom sound...)
Schedule / Create the LN
Do you have your queue maxConcurrentOperationCount set to 1?
The method setShouldContinueWhenAppEntersBackground:YES is set on a per request basis, and since you have a bunch of ASIHTTPRequest's inside a queue, only one of them may be executing at a time. This means that the other items in your queue haven't even started when you suspend the app, so the OS doesn't know yet to keep that network request alive.
I'm not sure of a solution, but I think this is the reason for what you're seeing.