Handling notifications in the BlackBerry Hub

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

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

Display a push message for an open invoke event

The sample app 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 invoke events
document.addEventListener("invoked", sample.pushcapture.onInvoke);

The onInvoke() function 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 app displays the push message that corresponds to the notification.

PushCapture.prototype.onInvoke = function (invokeRequest) {           
   if (invokeRequest.action != null && invokeRequest.action == 
      "bb.action.PUSH") {
      // Do something
   } 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 app to the BlackBerry Hub, the sample app 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",
   // Set the data of the invoke to be the seqnum of the 
   // push so that the app knows 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. Also, 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 app from the BlackBerry Hub, the sample app 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 app, and answers Yes to the confirmation message. The sample app also deletes the push message from the app.

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

Remove all notifications from the BlackBerry Hub

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

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

Last modified: 2015-06-11



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

comments powered by Disqus