+

Search Tips   |   Advanced Search

Globalization of push notifications

With IBM MobileFirst Platform Foundation, we can globalize push notifications so that push notifications are displayed in the language of the user. You use different methods to globalize push notifications, depending on the way the application runs: in the foreground, in the background, or not at all.

Mobile applications frequently rely on server-side services to provide data to the mobile application. However, sometimes the application is not running or is not connected to the server. Push notifications is a mechanism by which short messages are sent to let the user of a mobile application know data that can be downloaded or information that can be viewed from a server when the application is either not running or not running in the foreground.

iOS uses Apple Push Notification Service (APNS), Android uses Google Cloud Messaging (GCM), and Windows Phone uses Microsoft Push Notification Service (MPNS).

Translate push notification messages so the correct language is displayed to the user. You choose the architectural pattern depending on whether the application runs in the foreground, in the background, or not at all.

This diagram shows the following data flow:

  1. The service that generates the message uses the send resource key and delivers the message data to the messaging mediator.

  2. The messaging mediator uses the receive resource key to send the message data to the mobile application.

  3. The mobile application uses the resource key to extract the message text and deliver it to the language resource.

Figure 1. Use of resource keys

Listing 1, Listing 2, and Listing 3 show sample code that can be used when the mobile application is running in the foreground.

First, create a MobileFirst adapter to send the notification. For more information about how to create an adapter, see the tutorials on the Get Started page.


Listing 1: Send notification using created adapter

This listing shows how to send a notification with the created adapter. The target message, or resource key, to be translated on the client-side, is specified in the payload.

function sendNotificationOTA(userId, notificationText) {
  var userSubscription = WL.Server.getUserNotificationSubscription(
    'mysuranceAdapter.mysuranceEventSource', userId);
  WL.Server.notifyAllDevices(
    userSubscription, {
      badge : 1,   sound : "",   alert : notificationText,   payload : { globalizeString : 'notificationText' }
    }
  );
}


Listing 2: Client-side subscription code

This listing shows the code that is required on the client-side to subscribe to push notification.

WL.Client.Push.onReadyToSubscribe = function(){
  WL.Client.Push.registerEventSourceCallback(
    "mysurancePush", 
    "mysuranceAdapter", 
    "mysuranceEventSource", 
    pushNotificationReceived);
};

After successful subscription, the callback method is implemented. The callback method is responsible for retrieving the data from the payload, retrieving application locale preferences, retrieving the message using the resource key, and formatting the message.


Listing 3: Callback method

This listing shows how to retrieve the locale information and load the corresponding translated message with Dojo using the resource key stored in the payload object.

function pushNotificationReceived(props, payload){
  if (payload.globalizeString != "undefined"){
    require(
      ["dojox/mobile/i18n", "dojo/number"],   function(mi18n, number){
        bundle = mi18n.load("resource", "messages");
        // get globalization text by dojo mobile i18n
        var notificationText = bundle[payload.globalizeString];
        // format number by device locale         var num = number.format(1234567890, {
          places: 2, locale: WL.App.getDeviceLocale()});
        num = bundle["amount"] + num;
        //display globalization message
        alert(notificationText + "\n" +num);
    );
}

Figure 2. Sending push notifications according to the user's settings


Parent topic: Develop globalized hybrid applications