Configure a polling event source to send push notifications
Polling event sources can be used to generate notification events, such as push notifications, that the MobileFirst client framework can subscribe to.
The MobileFirst adapter framework provides the ability to implement event sources, which can be used to generate notification events such as push notifications. However, notifications must be retrieved from a back-end system before they can be sent out. Event sources can either poll notifications from the back-end system, or wait for the back-end system to explicitly push a new notification.
This task describes how to create a polling event source, and use it to send push notifications. A polling event source is a long-running task that has the following mandatory properties:
- Event source name
- Polling interval
- Polling function
- Consider the following simple example. The diagram shows a sample for a basic polling event source:
The doSomething() function is invoked every three seconds. If we deploy this adapter to the MobileFirst Server, you see the following logs in the server console:
The log shows that the doSomething() function is invoked at 3-second intervals.
- This second example shows a more realistic example of a polling event source:
The sample includes the following key elements:
- Lines 7 - 8: The polling event source continuously invokes a sendNotifications() function with a 3-second interval.
- Lines 18 - 19: Every time the sendNotifications() function is invoked it requests messages data from the back-end. The sample shows an HTTP back-end, but it could be any other type of back-end that MobileFirst adapters support; for example, SQL. The code assumes that the following JSON markup is returned by the back-end. However, since the MobileFirst adapter knows how to automatically convert data to JSON, the back-end data could also be XML.
{ messages: [{ userId: "John", text: "New incoming transition", badge: 2, payload: {} }, { userId: "Bob", text: "Please approve withdrawal", badge: 5, payload: {} }] }
- Line 22: The code iterates over the received messages array.
- Line 25: Every message contains the user ID of a user that the notification should be sent to.
- Line 28: Use this user ID, the code tries to obtain a userSubscription object.
- Lines 30 - 33: If a userSubscription object is found for the specified user ID, a new notification is created and is sent to all user devices.
- Line 35. If a userSubscription object is not found for the specified user ID, an error is logged.
An important feature of a polling event source is that unlike regular adapter procedures, the polling function is triggered by the MobileFirst Server itself, and not by the incoming request. Therefore any data or APIs related to request or session context are not available or functional. For example, APIs such as WL.Server.getActiveUser() or WL.Server.getClientRequest() are not functional. Also, you do not need to expose polling function in the adapter XML file.
Parent topic: Push notification