12

I have a single android application, which supports for 7 countries(Localization and Internationalization). The application functionality and language changed based on the device locale. I need to implement the GCM push notifications for this application. Requirement:

  • Is it possible to send the push notification in 7 different languages with single GCM account.
  • Is there any way to display the push notification in their device local language.
garg10may
  • 5,794
  • 11
  • 50
  • 91
Ganesh
  • 662
  • 3
  • 8
  • 26

5 Answers5

12

You can either take the approach suggested by Ascorbin, or implement something similar to what Apple have in their push notifications:

Your server can send a GCM message with a parameter that is a key to a message. Yout Android App will have to contain for each possible key the strings that should be displayed for it in each of the 7 languages (using multiple copies of strings.xml). Then the GCM reciever in your app will get the key from the server and get the resource string that matches it (it will automatically get the string that matched the locale of the device). This way you don't have to worry about localization in your server. The downside of this approach is that all your messages have to be predefined in your app.

You can also add parameters to the message key like Apple do. For example, the server sends a key = "NEW_MAIL_FROM" and param1 = "John". The app finds a string resource for that key (lets assume the device used English locale) - "You have a message from {0}" - and replaces the param with John, displaying the message "You have a message from John". A device with a differennt locale will show a message in a different language.

Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
Eran
  • 387,369
  • 54
  • 702
  • 768
  • One could also just send the message in 7 different languages and decide on the phone which to display. Would create some overhead, but push messages are pretty short in nature. – fweigl Mar 03 '14 at 19:02
  • @Ascorbin Your suggestion won't work if for some reason the message in the relevant language doesn't reach the device (which may happen if you send many messages to an inactive device in a short time period). – Eran Mar 03 '14 at 19:26
  • I meant to send all languages in one Gcm message. Dont know the size limit for those though. – fweigl Mar 03 '14 at 19:28
  • 2
    @Ascorbin The limit is 4k. It might work for a small number of languages, but it doesn't scale well. – Eran Mar 03 '14 at 19:31
  • 1
    This wouldn't work for notification messages, since the app will not be active to interpret the message. – PiersyP May 16 '16 at 18:14
  • @PiersyP Have you experienced this before? – Stavros_S Sep 07 '16 at 04:16
  • @Stavros No, i have not but it is documented here (note this is only for *notification* messages) - https://developers.google.com/cloud-messaging/concept-options#payload – PiersyP Sep 07 '16 at 11:28
  • Is there any guide out there explaining exactly how to setup the multiple copies of strings.xml? – Jon Nov 19 '20 at 02:03
2

You can implement that server-side, after GCM registration with the send of token, send also the device locale. And then notify users instantly with a localized message.

Payload is something "sort" its not a good idea to pass through it so much information.


On the other hand if you have fixed messages you can use:

private void handleMessage(Intent intent) {
    // server sent key-value pairs
    String name_of_resource = intent.getExtra("message_id");

    int id = getResources().getIdentifier(name_of_resource, "string", getPackageName());
    if (id != 0) {
         String text = getString(id); // the text to display
         // generates a system notification to display here
    }
}

see http://developer.android.com/google/gcm/gcm.html#received_data for handling received data.

madlymad
  • 6,367
  • 6
  • 37
  • 68
2

You can easily localize your GCM notification using title_loc_key and body_loc_key. These keys listed in official GCM docs.

More details can be found here.

Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
1

When the devices register at your server, let them send the Locale. So you can have locale groups of devices and send the messages in according languages.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • 1
    Yes, but what if between the time when the device token is sent to the server and the time when we decide to send the user a notification, the user changes his language? On the server we still have the old language, sent along with the token, a while back, not the current language. In this case, the application might send the server the current language every time the user changes the locale. – Alex Bitek Mar 03 '14 at 16:45
  • True, but how often will that be the case? How often do you change the language of your phone? – fweigl Mar 03 '14 at 19:01
  • It does not matter how often, it can just happen. Other answers suggest receive "keys" within the notification and making them match localized string resources, which is a much better approach (neat, simple, and bulletproof). – German Latorre Mar 27 '14 at 09:44
  • This approach is wrong, bcoz push need not always be sent to the user who has made the request. Let's say a user shares something with you and the push is sent to you. In this case you would start seeing quirky push in his language. – garg10may Sep 29 '17 at 06:51
  • @garg10may The answer explicitly states that the backend would have to keep a group of registered devices for every locale. Every device is unambiguously connected to it's locale via these groups, so the backend has exact control which push it sends to which device. – fweigl Sep 29 '17 at 08:29
  • First is, how will you map each device, if you use some unique ID, like user, the same user can be logined to multiple devices. Secondly one can change the language any time after registration. – garg10may Sep 29 '17 at 09:05
  • Update the server on locale changes.. thats not a problem – Vlad Aug 17 '18 at 06:58
-1
  1. Send a GCM Push from server (without any language specific data).
  2. In response to the push, the client makes a REST api call to the server with it's language as a Query parameter.
  3. The server fetches appropriate language's text and send back to the client on real time.
Michael Massey
  • 469
  • 1
  • 7
  • 16