Handling notifications in the BlackBerry Hub

The sample application handles notifications of push messages in the BlackBerry Hub in the following ways:

  • When a user taps on a notification in the BlackBerry Hub, the sample application receives an open invoke event, and displays the push message that corresponds to the notification.
  • When the sample application receives a push message, it adds a notification to the BlackBerry Hub.
  • When a user taps on the push message in the sample application, the application removes the notification from the BlackBerry Hub.
  • When a user deletes an individual push message in the sample application, the application removes the notification for the push message from the BlackBerry Hub.
  • When a user taps Mark All Open or Delete All in the sample application, the application removes all the notifications for the sample application from the BlackBerry Hub.

Display a push message for an open invoke event

The sample application registers an event listener that listens for invoke events. An open invoke event occurs when a user taps a notification for a push message in the BlackBerry Hub.

// Add an event listener to handle incoming invokes
blackberry.event.addEventListener("invoked", sample.pushcapture.onInvoke);

The onInvoke() function first checks the action property of the invokeRequest to see if it matches the string value bb.action.OPEN. If the property matches the string value, the user tapped a notification in the BlackBerry Hub. The sample application displays the push message that corresponds to the notification.

PushCapture.prototype.onInvoke = function (invokeRequest) {           
   if (invokeRequest.action != null && invokeRequest.action == 
      "bb.action.PUSH") {
    .
    .
    .
   } else if (invokeRequest.action != null && invokeRequest.action == 
         "bb.action.OPEN") {
      // The payload from the open invoke is the seqnum for the push  
      // in the database.
      var payload = sample.pushcapture.b64_to_utf8(invokeRequest.data);
                 
      if (sample.pushcapture.isDomReady) {
         if (document.getElementById("push-list") == null) {
            // We are not on the home screen.
            // Pop off the current screen so that the push can  
            // be opened properly.
            bb.popScreen();

            // We have to wait until the pop finishes and the home  
            // screen loads before we can open the push, so we do  
            // it after the pushes are displayed by the call to 
            // initPushList() in pushlist.js.
            sample.pushcapture.isOpenInvoke = true;
            sample.pushcapture.selectedPushSeqnum = payload;
         } else {
            // We are on the home screen, simply open the push.
            sample.pushcapture.openPush(payload);
         }
      } else {
         // The home screen has not loaded up yet. We need to wait 
         // until it loads before we can open the push, so we do it 
         // after the pushes are displayed by the call to 
         // initPushList() in pushlist.js.
         sample.pushcapture.isOpenInvoke = true;
         sample.pushcapture.selectedPushSeqnum = payload;
      }
   }
};

Add a notification to the BlackBerry Hub

To add a notification for the sample application to the BlackBerry Hub, the sample application creates a Notification object.

// Add a notification to the BlackBerry Hub for this push
var title = "Push Capture";
var options = {
   body: "New " + extension + " push received",
   tag: seqnum + sample.pushcapture.notificationSuffix,
   target: sample.pushcapture.invokeTargetIdOpen,
   targetAction : "bb.action.OPEN",
   // We set the data of the invoke to be the seqnum of the 
   // push so that we know which push needs to be opened
   payload : sample.pushcapture.utf8_to_b64(seqnum.toString(10))
};
new Notification(title, options);

Notice that targetAction matches the bb.action.OPEN action in the config.xml file. The sample.pushcapture.invokeTargetIdOpen constant has a value of sample.pushcapture.invoke.open which matches the invoke-target id in the config.xml file. For more information about the config.xml file, see Update your config.xml file. Additionally, when you create a notification, you need to encode the payload that you specify in Base64.

Remove a notification from the BlackBerry Hub

To remove a notification for the sample application from the BlackBerry Hub, the sample application calls Notification.remove(), and passes in the sequence number of the push message that needs to be removed. This code is called when the user taps the trash can icon next to a push message in the sample application, and answers Yes to the confirmation message. The sample application also deletes the push message from the application.

Notification.remove(sample.pushcapture.selectedPushSeqnum +
   sample.pushcapture.notificationSuffix);

Remove all notifications from the BlackBerry Hub

To remove all the notifications for the sample application from the BlackBerry Hub, the sample application calls Notification.remove() with the sequence number of each push message in the sample application. This code is called when the user taps either Mark All Open or Delete All in the sample application.

for (var i = 0; i < results.rows.length; i++) {
     Notification.remove(results.rows.item(i).seqnum + 
       sample.pushcapture.notificationSuffix);      
}

Last modified: 2014-03-10



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

comments powered by Disqus