Android GCM push notification for localization application
Asked Answered
Q

5

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.
Quest answered 19/2, 2013 at 13:13 Comment(0)
F
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.

Forenamed answered 19/2, 2013 at 13:29 Comment(8)
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.Reglet
@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).Forenamed
I meant to send all languages in one Gcm message. Dont know the size limit for those though.Reglet
@Ascorbin The limit is 4k. It might work for a small number of languages, but it doesn't scale well.Forenamed
This wouldn't work for notification messages, since the app will not be active to interpret the message.Leticialetisha
@Leticialetisha Have you experienced this before?Arapaima
@Stavros No, i have not but it is documented here (note this is only for notification messages) - developers.google.com/cloud-messaging/concept-options#payloadLeticialetisha
Is there any guide out there explaining exactly how to setup the multiple copies of strings.xml?Grab
S
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.

Selfexplanatory answered 19/2, 2013 at 13:29 Comment(0)
I
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.

Impression answered 20/12, 2018 at 10:57 Comment(0)
R
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.

Reglet answered 19/2, 2013 at 13:15 Comment(7)
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.Ostwald
True, but how often will that be the case? How often do you change the language of your phone?Reglet
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).Stumper
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.Aerodyne
@Aerodyne 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.Reglet
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.Aerodyne
Update the server on locale changes.. thats not a problemInterlocutress
R
-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.
Rotl answered 23/9, 2015 at 7:55 Comment(2)
why would i want to make two calls per GCM push.Rickart
Why is this downvoted so much? It's a legit solutionInterlocutress

© 2022 - 2024 — McMap. All rights reserved.