简体   繁体   中英

Android GCM push notification for localization application

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.

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.

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.

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.

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 .

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM