Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UiLocalNotification formatting

I am adding local notifications to my current project and I noticed something weird. What I am doing is displaying warnings when a piece of hardware has a low battery. When the app is in the foreground (and I replace the notification with an alert view) the string displays fine. However when the app is in the background my percentile sign disappears.

    @"%@ has less than %i%% battery left!"

is the string that I am using. In the alert view it will display something like "X has less than 10% battery left!" but the background notification displays "X has less than 10battery left!"

Has anyone encountered that before?

edit (adding code snippet)

UILocalNotification *lc = [[UILocalNotification alloc] init];
lc.fireDate = [[NSDate alloc]init];
lc.alertBody = [NSString stringWithFormat:@"%@ has less than %i%% battery left!", name, percentage];

lc.timeZone = [NSTimeZone defaultTimeZone];
lc.applicationIconBadgeNumber = lc.applicationIconBadgeNumber + 1;

[[UIApplication sharedApplication] scheduleLocalNotification:lc];

Where name is a NSString and percentage is an int

like image 639
Pinsickle Avatar asked Mar 20 '23 16:03

Pinsickle


2 Answers

I found out that using the unicode character equivalent will make it work in both the alert view and notification. I changed my string to:

@"%@ has less than %i\uFF05 battery left!"

However, I'd still like to know why escaping doesn't work on a local notification.

like image 112
Pinsickle Avatar answered Mar 31 '23 18:03

Pinsickle


I have reproduced the issue in my simulator also and also I fixed the issue.

Use:

@"%@ has less than %i %%%% battery left!"

or

[NSString stringWithFormat:@"%@ has less than %i %@ battery left!",@"Your App",7,@"%%"]

Instead of:

@"%@ has less than %i%% battery left!"
like image 44
Midhun MP Avatar answered Mar 31 '23 17:03

Midhun MP