localization in iOS notifications? - iphone

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

Related

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.

iPhone: How to get notification center to detect date?

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.

How can I identify UILocalNotification and snooz it again?

I am developing an alarm app and having some issues and snooz and repeatation of alarms, I am making a Class having name, time and other alarm options below
int alarm_id;
NSString *nameOfAlarm;
NSString *timeOfAlarm;
NSString *repeatAlarm;
NSString *soundOfAlarm;
NSString *snoozOfAlarm;
NSString *soundFadeInOfAlarm;
NSString *volumeOfAlarm;
NSString *vibrationOfAlarm;
Now I am saving above values to my Sqlite database, and at same time I am setting notification to iOS, when notification fires the, then I want to snooz and also want same alarm on different days,
I don't want exact code, but a concept or view how to do that ?
Help .
You can use UILocalNotification's userInfo to store a unique identifier for each notification.
Take a look at the UILocalNotification documentation.

Is there any way to add identifier or tag custom event added in iCal?

I am setting a reminder in my app. I have added a custom event using EKEvent to iCal. Now when I retrieve events from iCal I get all the events present on that day. Is there any way to get/retrieve events added through my app only, I tried eventIdentifier property of EKEvent but it is a readonly property.
Can anybody help???
You could loop through all of the calendar events that match a specific date but that is not the preferred method. Each event is created with a unique eventIdentifier property. When you save the event you can copy the eventIdentifier and next time you want to modify that specific event you can use the EKEventStore eventWithIdentifier Method to load your Event.
A sample might look like this
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSError *err;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
//modify all the event properties you would like then save
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
self.calendarEventID = event.eventIdentifier;
[eventStore release];
Later if you want to retrieve the saved event from the previous code you could do the following
//self.calendarEventID is a NSString property declared in the .h file and synthesized in .m
EKEvent *myEvent = [eventStore eventWithIdentifier:self.calendarEventID];
Kludge:
I had a similar problem with an AppleScript I made for setting iCal alarms; I wanted to be able to identify and delete the events my script had made on the next pass.
I couldn't find any tag-like properties for iCal events, so I ended up using the location property, which is a string; I set it to " " and searched for that. (Caveat: The alarm message includes the location at the end, surrounded by parens, so this glops things up a bit.)
If you need the location property for other purposes in your app, you still might be able to add some identifying character sequence. Or maybe you can use some other property you don't otherwise need.