Event Source Notifications in Native Windows 8 Applications

improve this page | report issue

Overview

Prerequisite: Make sure that you read the Push notifications in native Windows 8 applications tutorial first.

Event source notifications are notification messages that are targeted to devices with a user subscription.
While the user subscription exists, MobileFirst Server can produce push notifications for the subscribed user. These notifications can be delivered by the adapter code to all or some of the devices from which the user subscribed.

To learn more about the architecture and terminology of event-source push notifications refer to the Push notification overview tutorial.

Implementation of the push notification API consists of the following main steps:

On the server side:

  • Creating an event source
  • Sending notification

On the client side:

  • Sending the token and initializing the WLPush class
  • Registering the event source
  • Subscribing to/unsubscribing from the event source

Agenda

Notification API - Server-side

Creating an event source

To create an event source, you declare a notification event source in the adapter JavaScript code at a global level (outside any JavaScript function):

WL.Server.createEventSource({
    name: 'PushEventSource',
    onDeviceSubscribe: 'deviceSubscribeFunc',
    onDeviceUnsubscribe: 'deviceUnsubscribeFunc',
    securityTest:'PushApplication-strong-mobile-securityTest'
});

  • name – a name by which the event source is referenced.
  • onDeviceSubscribe – an adapter function that is invoked when the user subscription request is received.
  • onDeviceUnsubscribe – an adapter function that is invoked when the user unsubscription request is received.
  • securityTest – a security test from the authenticationConfig.xml file, which is used to protect the event source.

An additional event source option:

poll: {
    interval: 3,
    onPoll: 'getNotificationsFromBackend'
}

  • poll – a method that is used for notification retrieval.
    The following parameters are required:

    • interval – the polling interval in seconds.
    • onPoll – the polling implementation. An adapter function to be invoked at specified intervals.


Sending a notification

As described previously, notifications can be either polled from the back-end system or pushed by one. In this example, a submitNotifications() adapter function is invoked by a back-end system as an external API to send notifications.

function submitNotification(userId, notificationText) {
    var userSubscription = WL.Server.getUserNotificationSubscription('PushAdapter.PushEventSource', userId);
    if (userSubscription === null) {
        return { result: "No subscription found for user :: " + userId };
    }
    var badgeDigit = 1;
    var notification = WL.Server.createDefaultNotification(notificationText, badgeDigit, {custom:"data"});
        WL.Server.notifyAllDevices(userSubscription, notification);
    return {
        result: "Notification sent to user :: " + userId
    };
}

The submitNotification function receives the userId to send notification to and the notificationText.

function submitNotification(userId, notificationText) {

A user subscription object contains the information about all of the user’s subscriptions. Each user subscription can have several device subscriptions. The object structure is as follows:

{
    userId: 'bjones',
    state: {
        customField: 3
    },
    getDeviceSubscriptions: function()[}
};

Next line:

var userSubscription = WL.Server.getUserNotificationSubscription('PushAdapter.PushEventSource', userId);

If the user has no subscriptions for the specified event source, a null object is returned.

if (userSubscription === null) {
    return { result: "No subscription found for user :: " + userId };
}

The WL.Server.createDefaultNotification API method creates and returns a default notification JSON block for the supplied values.

var badgeDigit = 1;
var notification = WL.Server.createDefaultNotification(notificationText, badgeDigit, {custom:"data"});

  • notificationText - The text to be pushed to the device.
  • Badge (number) - A number that is displayed on the application icon or tile (in environments that support it).
  • custom - Custom, or Payload, is a JSON object that is transferred to the application and that can contain custom properties.

The WL.Server.notifyAllDevices API method sends notification to all the devices that are subscribed to the user.

WL.Server.notifyAllDevices(userSubscription, notification);


Several APIs exist for sending notifications:

  • WL.Server.notifyAllDevices(userSubscription, options) - to send notification to all user’s devices.
  • WL.Server.notifyDevice(userSubscription, device, options) - to send notification to a specific device that belongs to a specific user subscription.
  • WL.Server.notifyDeviceSubscription(deviceSubscription, options) - to send the notification to a specific device.

Notification API - Client-side

The first step is to create an instance of the WLClient class:

WLClient client = WLClient.getInstance();

You derive all push notification operations from the WLPush class.

    getPush – Use this method to retrieve an instance of the WLPush class from the WLClient instance.

    WLPush push = client.getPush();

    WLOnReadyToSubscribeListener – When connecting to MobileFirst Server, the application attempts to register itself with the Google Cloud Messaging (GCM) server to receive push notifications.

     OnReadyToSubscribeListener myOnReadyListener = new OnReadyToSubscribeListener();
     push.onReadyToSubscribeListener = myOnReadyListener;

    The onReadyToSubscribe method of WLOnReadyToSubscribeListener is called when the registration is complete.

    public void onReadyToSubscribe()
    {...}

WLPush.registerEventSourceCallback

To register an alias on a particular event source, use the WLPush.registerEventSourceCallback method.

The API takes the following arguments:

    alias - An alias name.
    Adaptername - Adapter in which the event source is defined.
    EventSourceName - The event source on which the alias is called.

Example:

WLClient.getInstance().getPush().registerEventSourceCallback("myPush", "PushAdapter", "PushEventSource", this);

Typically, this method is called in the onReadyToSubscribe callback function.

<
public void onReadyToSubscribe()
{<br />
  WLClient.getInstance().getPush().registerEventSourceCallback("myPush", "PushAdapter","PushEventSource", this);<br />
}


Subscribing to push notification

To set up subscription to push notification, use the WLPush.subscribe(alias, pushOptions, responseListener) API.

The API takes the following arguments:

    alias – The alias to which the device must subscribe.
    pushOptions – An object of type WLPushOptions.
    responseListener – An object of type WLResponseListener, which is called when subscription completes.

Example:

WLPush push = WLClient.getInstance().getPush();
MySubscribeListener mySubListener = new MySubscribeListener();
push.subscribe("myPush", null, mySubListener);

MySubscribeListener implements WLResponseListener and provides the following callback functions:

    onSuccess – Called when subscription succeeds.
    onFailure – Called when subscription fails.

Unsubscribing from push notifications

To set up unsubscription from push notification, use the WLPush.unsubscribe(alias, responseListener) API.

The API takes the following arguments:

    alias – The alias to which the device has subscribed.
    responseListener – An object of type WLResponseListener, which is called when unsubscription completes.

Example:

WLPush push = WLClient.getInstance().getPush();
MyUnsubscribeListener myUnsubListener = new MyUnsubscribeListener();
push.unsubscribe("myPush", myUnsubListener);

MyUnsubscribeListener implements WLResponseListener and provides the following callback functions:

    onSuccess – Called when unsubscription succeeds.
    onFailure – Called when unsubscription fails.

Additional client-side API methods

isSubscribed() - Indicates whether the device is subscribed to push notifications.

WLClient.getInstance().getPush().isSubscribed("myPush");

Receiving a push notification

When a push notification is received, the onReceive method is called on an WLEventSourceListener instance.

class OnReadyToSubscribeListener : WLOnReadyToSubscribeListener, WLEventSourceListener{...}

The WLEventSourceListener instance is registered during the registerEventSourceCallback callback.

WLClient.getInstance().getPush().registerEventSourceCallback("myPush", "PushAdapter", "PushEventSource", this);

The onReceive method displays the received notification on the screen.

public void onReceive(String props, String payload)
{
     Debug.WriteLine("Props: " + props);
     Debug.WriteLine("Payload: " + payload);
<p>}

Sample application

Click to download the MobileFirst project.

Click to download the Native project.

  • The EventSourceNotifications project contains a MobileFirst native API that you can deploy to your MobileFirst Server instance.
  • The EventSourceNotificationsWin8 project contains a native Windows 8 Universal application that uses a MobileFirst native API library to subscribe to push notifications and receive notifications from Windows Notification Services (WNS).
  • Make sure to update the wlclient.properties file in the native project with the relevant server settings.

Sending a notification

To test the application is able to receive a push notification you can perform one of the following:

  1. Right-click the adapter in MobileFirst Studio and select Call MobileFirst Adapter
  2. If using the CLI, for example:
    $ mfp adapter cal
    [?] Which endpoint do you want to use? PushAdapter/submitNotification
    [?] Enter the comma-separated parameters: "the-user-name", "hello!"
    [?] How should the procedure be called? GET
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on November 09, 2016