Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send localized string key in loc-args for iOS Push notifications

My application supports 4 languages & push notifications. When I send push notification to APNS , I am sending loc_key & loc-args. Now I need to send localized strings in loc-args array so that I can translate those on iOS app side when app receives the push notification.

But when I send localized strings in loc-args , instead of showing translated string in notification center , it just showed localized key as it is.

My string file contains below 2 messages:

"WINNER_ALERT"= "Congratulations! %@ won the match & became %@ player";
"ROLE_PROFESSIONAL_LOCALIZED_KEY" = "professional"

Server sends below payload

{
    aps =     {
        alert =         {
            "loc-args" =             (
                "John",
                "ROLE_PROFESSIONAL_LOCALIZED_KEY"
            );
            "loc-key" = "WINNER_ALERT";
        };
        badge = 1;
        sound = default;
    };
}

When I send above payload then in iOS Notification Center , message look like

Congratulations! John won the match & became ROLE_PROFESSIONAL_LOCALIZED_KEY player

instead of

Congratulations! JOHN won the match & became professional player

Can anyone tell me whether it is possible to send localized strings in loc-args ? If yes, what's wrong in my payload ?

Thanks in advance

like image 521
iOSAppDev Avatar asked Aug 07 '14 10:08

iOSAppDev


People also ask

How do I localize push notifications?

I know that it is possible to localize the message and title of a push notification, using title-loc-key , title-loc-args and loc-key , loc-args . So, that the system searches for the localized-title-formatted-string( title-loc-key ) in the app bundle and substitutes the format-specifiers with title-loc-args .

What is payload in push notification iOS?

The payload contains any custom data that you want to send to your app and includes information about how the system should notify the user. You construct this payload as a JSON dictionary and send it as the body content of your HTTP/2 message.

What is loc-key?

loc-key - A key to an alert-message string in a Localizable. strings file for the current localization (which is set by the user's language preference). The loc-key and loc-args keys define the message content of the notification.

What is iOS payload?

A software payload allows you to distribute an app to the device. The payload sends the app information and location to the device. If you select the option to automatically push the app to the device, the device displays a notification to prompt the user to download the app when it receives the Avalanche payload.


1 Answers

I believe your Localizable.strings file should look like this :

"WINNER_ALERT"= "Congratulations! %@ won the match & became %@ player";

And the push notification payload should look like this :

{
    aps =     {
        alert =         {
            "loc-args" = (
                "JOHN",
                "professional"
            );
            "loc-key" = "WINNER_ALERT";
        };
        badge = 1;
        sound = default;
    };
}

This will give the desired result :

Congratulations! JOHN won the match & became professional player


Here's why (according to Apple's Local and Remote Notification Programming Guide) :

  • loc-key : A key to an alert-message string in a Localizable.strings file for the current localization
  • loc-args : Variable string values to appear in place of the format specifiers in loc-key
like image 191
Zakaria Braksa Avatar answered Nov 02 '22 23:11

Zakaria Braksa