Event source-based notifications in Hybrid applications
OverviewEvent source notifications are notification messages that are targeted to devices with a user subscription. This tutorial explains the concept, API, and usage of push notifications in the context of hybrid applications. The following topics are covered:
- Notification architecture
- Sending notifications
- Subscription management
- Notification API
- Back-end emulator
Event sourceA push notification channel to which mobile applications can register. An event source is defined within a MobileFirst adapter.
Device tokenA unique identifier, obtained from the push mediator (Apple, Google, or Microsoft), which identifies the request of a specific mobile device to receive notifications from the MobileFirst Server.
User IDA unique identifier for a user. Obtained through authentication or other unique identifier such as a persistent cookie.
Application IDMobileFirst application ID. Identifies a specific MobileFirst application on the mobile market.
SubscriptionTo start receiving push notifications, an application must first subscribe to a push notification event source. An event source is declared in the MobileFirst adapter that is used by the application for push notification services. The end user must approve the push notification subscription. After the subscription is approved, the device registers with an Apple, Google, or Microsoft push server to obtain a token that is used to identify the device (“Allow notifications for application X on device Y”), and sends a subscription request to the MobileFirst Server. This operation is performed automatically by the MobileFirst framework.
subscribeAPI method is called, the device registers with a push service mediator and obtains a device token (done automatically by the framework).
When the token is obtained, the application subscribes to an event source. Both actions are done automatically by a single API method call as described later.
Sending notificationsIBM MobileFirst Platform Foundation provides a unified API for push notification. Use the adapter API for the following actions:
- Managing subscriptions
- Pushing and polling notifications from a back end
- Sending push notifications to devices
- Subscribing to and unsubscribing from push-notification event sources
- Handling incoming notifications
DemonstrationNotifications are retrieved by the MobileFirst adapter event source, either by poll or by push from the back-end system.
The adapter processes the notification and sends it to a push service mediator.
The push service mediator sends a push notification to the device.
The device processes the received notification.
SubscriptionAn entity that contains a user ID, a device ID, and an event source ID. It represents the intent of the user to receive notification from a specific event source.
CreationThe user subscription for an event source is created when the user first subscribes to the event source from any device.
DeletionA user subscription is deleted when the user unsubscribes from the event source from all the user’s devices.
NotificationWhile the user subscription exists, the 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 that the user subscribed from.
Device subscriptionA device subscription belongs to a user subscription and exists in the scope of a specific user and event source. A user subscription can have several device subscriptions. The device subscription is created when the application on a device calls
WL.Client.Push.subscribe(). The device subscription is deleted either by an application that calls
WL.Client.Push.unsubscribe(), or when the push mediator informs the MobileFirst Server that the device is permanently not accessible.
Notification API - Server-side
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.xmlfile, which is used to protect the event source.
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 notificationAs 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. The
submitNotificationfunction receives the
userIdto send notification to and the
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: Next line: If the user has no subscriptions for the specified event source, a null object is returned. The
WL.Server.createDefaultNotificationAPI method creates and returns a default notification JSON block for the supplied values.
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.
WL.Server.notifyAllDevicesAPI method sends notification to all the devices that are subscribed to the user.
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-sideAdditional client-side API methods:
trueif push notifications are supported by the platform, or
WL.Client.Push.isSubscribed(alias)– returns whether the currently logged-in user is subscribed to a specified event source alias. The call does not connect to the server, it returns the local state.
WL.Client.Push.registerEventSourceCallbackis invoked: If the application was in background mode (or inactive) when the push notification arrived, this callback function is invoked when the application returns to the foreground.
Back-end emulatorThe sample project for this tutorial is bundled with a back-end emulator which can be used to simulate push notification submissions by a back-end system. The source for the emulator can be found in the sample project. To run the back-end emulator, open the PushBackendEmulator folder of the sample project in a command prompt, and then run the supplied JAR file by using the following syntax:
userIdis the user name that you used to log in to the sample application.
contextRootis the context root of your MobileFirst project.
ExampleThe back-end emulator tries to connect to a MobileFirst Server and call a
submitNotification()adapter procedure. It outputs the invocation result to a command prompt console.
Sample applicationClick to download the Studio project. ▲
Last modified on November 28, 2017