3

When I'm trying to display a notification in iOS, the characters "% " get removed.

The text I am trying to display is "51% ", but all I get is "50".

Any ideas why?

I've check in the debugger and put a breakpoint where I see what my message is supposed to say before being sent as a notification and it properly says "51% ", but when it's displayed via notification, it gets chopped off.

Arthur Garza
  • 2,031
  • 2
  • 14
  • 17
  • 2
    % is often used as a placeholder for value substituitions. Have you tried typical escape patterns such as %% or \%? – bitfiddler Jan 14 '16 at 01:39
  • Most likely your notification makes use of some obj-c object (e.g. `NSString`): http://stackoverflow.com/questions/739682/how-to-add-percent-sign-to-nsstring – dfrib Jan 14 '16 at 02:04

1 Answers1

10

Might be interpreting the % as a printf formatter. Try using %%, as explained in the UNNotificationContent documentation of property body:

If you specified two percent symbols (%%) in the message body, the system replaces it with a single percent symbol (%). The system strips all other printf style escape characters from your string prior to display.

Martijn
  • 429
  • 4
  • 13
Thunk
  • 4,099
  • 7
  • 28
  • 47
  • 2
    Be careful, the old `UILocalNotification` API requires you to escape this character, however the new `UNNotificationContent` class does it automatically (you don't need to escape any character). In my personal case, after migrating to the UNNotificationCenter API, the notifications containing the `%` character started to appear with the duplicate symbol because I was escaping it for the deprecated version, – tomacco May 22 '18 at 13:15
  • 1
    The `UNNotificationContent` documentation of property `body` clearly states: "If you specified two percent symbols (%%) in the message body, the system replaces it with a single percent symbol (%). The system strips all other printf style escape characters from your string prior to display." (see: https://developer.apple.com/documentation/usernotifications/unnotificationcontent/1649863-body ). So the comment from @tomacco is wrong and confusing, since escaping is required for `UNNotificationContent`. – Martijn Apr 26 '21 at 16:04