UILocalNotification in iOS 5 - ios5

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

Related

iOS 8: Using timer in the background

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

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 to notify user in ios, while app is closed/ inbackground

i want to notify the user when a "time" is reached.
for example:
i have an tableview with many rows. this rows are something like reminders where the user can set the date and time of remind.
So in a row the reminder is set to 24 April 12:15.
At this time the App is closed.
So how can i notify the user that this time is reached?
Is there a way to do that without apple push notification service?
Edit 1:
Thanks for answering, here is an example for local notifications:
//This example adds 20 seconds to current time, use it for testing
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *currentDatePlus = [currentDate dateByAddingTimeInterval:20];
localNotification.fireDate = currentDatePlus;
localNotification.alertBody = #"Hallo ayFon User";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
EDIT 2:
//Add this for iOS 5 Notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
You have to use the Local Notifications of iOS. They are done in this exact purpose.
You can find documentation here: Local and Push Notification Programming Guide.
You don't have to use Push Notifications - you could use Local Notifications instead. They are similar to Push notifications except that they are generated on the device instead so you don't need a network connection.
This is a fairly common pattern for alarm type apps.

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.