iPhone: How to get notification center to detect date? - iphone

I just wrote an application that depends largely on the ability to notify you when a deadline is approaching. It allows the user to type in a specific date in a UITextField (not using a date picker.) I would rather not go through the hassle of setting up push notifications, iOS 5's notification center will work great. But, I'm unsure of how to get the notification center to detect the entered in dates.
I put this code in my didFinishLaunchingWithOptions: method in my view controller:
UILocalNotification *dateNotif = [[UILocalNotification alloc] init];
localNotif.fireDate=[NSDate date];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event starts in 20 minutes!";
localNotif.alertAction = #"Show me";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:dateNotif];
[localNotif release];
But I'm not sure where to go from here at all, I'm still really new to this. Any help would be great, thank you!

You need to create a UILocalNotification, set the fireDate appropriately (and set whatever text you want the user to see), then pass it to UIApplication's scheduleLocalNotification:.
The only things you'll need to deal with beyond that are that if your app is already running when the notification fires then the notification centre won't do anything and your application delegate will get a didReceiveLocalNotification:. If the app isn't running but the user chooses to launch it from the alert then you'll get the local notification handed back to your didFinishLaunchingWithOptions:, allowing you to do something relevant.

Related

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.

Display UIAlertView on certain date?

I was wondering how I'd make a UIAlertView display on a certain date, for example, users download the iPhone application and then on the 12th August, a predetermined UIAlertView displays? How could I do this?
Thanks!
You should take a look at the UILocalNotification API. It allows you to create and schedule local notification for your application, which will then be fired on the desired date, using a badge/popover/sound (same settings as for the remote notification API).
For your case you should specifically take a look at the fire date. Sample code would look moreless like this:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Your message here";
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60*60];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
This will fire a local notification with a "Your message here" text in one hour.

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 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.

localization in iOS notifications?

I have my app running nicely and it uses local notifications.
I have decided to internationalize the app now and have everything working just fine except notifications which were set on a language before changing the language on the device.
I populate the messages in the notification from an array which contains localized strings, so I figured that when the user changes the language of the device the string in the notification would also change but I was wrong.
How best to tackle this issue? Should my NSString text also be NSLocalizationString ?
My notification code:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [alertTimes objectAtIndex:i];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSString *text = [alertText objectAtIndex:i];
// Notification details
localNotif.alertBody = text;
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
Should my NSString text also be NSLocalizationString?
Yes, I would do that.
Replace [alertTimes objectAtIndex:i] with NSLocalizedString(#"alertTimes",[alertTimes objectAtIndex:i]). I am assuming that you storing strings in the alertTimes array that match your localized string.
Always use localizedUserNotificationString(forKey:arguments:) when localizing local notifications.
The "gotcha" with localization is that the static string from NSLocalizedString would not work well because there is the possibility where the user might switch a language after notification had been scheduled. It would result in the wrong language get displayed in a notification alert.
For example, a notification is scheduled in en-US with English copy string been set, before the notification gets triggered the user switched the language to jp since a static string was generated using NSLocalizedString there is no way it can be changed before notification get triggered.
localizedUserNotificationString(forKey: arguments:) should be used to localize string text for a notification content, https://developer.apple.com/documentation/foundation/nsstring/1649585-localizedusernotificationstring. Where a localized string value is created dynamically from a localized string resource when the notification is about to be displayed.
Just came across the same issue and found that
Apple Docs for UNMutableNotificationContent say to use the NSString. localizedUserNotificationString(forKey:arguments:) function, which defers loading the localized string till the notification shows up. That way the string will be properly localized even if the user changes languages between scheduling the notification and the notification being delivered.
"The strings you display in a notification alert should be localized for the current user. Although you can use the NSLocalizedString macros to load strings from your app’s resource files, a better option is to specify your string using the localizedUserNotificationString(forKey:arguments:) method of NSString. The localizedUserNotificationString(forKey:arguments:) method delays the loading of the localized string until the notification is delivered. Thus, if the user changes language settings before a notification is delivered, the alert text is updated to the user’s current language instead of the language that was set when the notification was scheduled."`