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.
- When the application is running in the foreground, it uses the language and cultural settings on the device to determine the appropriate language to display. To support this pattern, messages must be stored in the resource files of the mobile application, and not in the resource files of the server application, even though messages are generated on the server-side.
- When a notification is sent to a mobile application, send the notification resource key and not the actual text of the message.
- When a mobile application receives the notification, or message, use the key sent in the notification message to look up the text of the message from its resource file, as shown in Figure 1.
This diagram shows the following data flow:
- The service that generates the message uses the send resource key and delivers the message data to the messaging mediator.
- The messaging mediator uses the receive resource key to send the message data to the mobile application.
- 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); ); }
- If a notification provides data in addition to the message, send the data in a locale-neutral format. When the application retrieves the message, the data can be formatted based on the user cultural preferences at the time the message is received.
- An application running in the background, or not running at all, can elect to use a previously registered user profile to access the appropriate language and cultural settings for push notifications. To support this pattern, the server sends the translated message and data in a format determined by the user cultural and language preferences stored in the profile, as shown in Figure 2. The push notifications are then processed differently by the mobile application. Processing is based on the native operating system that the application is running on.
Figure 2. Sending push notifications according to the user's settings
![]()
- On Android, notification messages wake up Android applications, and the applications directly access the language and cultural preferences so the correct translation and formatting can be applied.
- On iOS, notification messages do not wake up iOS applications, therefore the native operating system automatically selects the appropriate language to use for notifications. The iOS operating system automatically attempts to locate and load the correct language resource.
- In hybrid applications built using MPF, notifications are not directly processed by the application when the application is not running in the foreground. In this case, the user language and cultural profile that was previously established is used.
Parent topic: Develop globalized hybrid applications