Receiving a push message

The sample application registers an event listener that listens for invoke events. The invoke event can be a push invoke event or an open invoke event. A push invoke event occurs when the application receives a push message. An open invoke event occurs when a user taps a notification for a push message in the BlackBerry Hub. The following code sample shows you how the sample application handles a push invoke event. To see how the sample application handles an open invoke event, see Handling notifications in the BlackBerry Hub.

This is the event listener that the sample application registers.

The onInvoke() function first checks the action property of the invokeRequest to see if it matches the string value bb.action.PUSH. The function then calls extractPushPayload() which returns a PushPayload object. The application processes the PushPayload object by calling pushNotificationHandler(). In your application, you can provide the code to process the push message in a way that's appropriate for your environment.

PushCapture.prototype.onInvoke = function(invokeRequest) {       
    if (invokeRequest.action != null && invokeRequest.action == 
             "bb.action.PUSH") {
       var pushPayload = sample.pushcapture.pushService.
                          extractPushPayload(invokeRequest);                   
       sample.pushcapture.pushNotificationHandler(pushPayload);
    } else if (invokeRequest.action != null && invokeRequest.action == 
             "bb.action.OPEN") { 
    .
    .
    .
   }
 };

The push message is stored in the data field of the PushPayload object. The type of the data field is Blob. You need to convert the push message from a Blob object to a format that you can work with. The API Reference shows you how to convert the Blob object to binary data and plain text.

You should also look at the sample.pushcapture.pushNotificationHandler() function in the sample application code for examples of how to check for duplicate push messages, how to store push messages in the database, and how to convert the Blob object to plain text and Base64 encoded strings for binary data.

Last modified: 2014-10-09



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus