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 
InvokeManager.invokeManager.addEventListener(InvokeEvent.INVOKE, 
   invokeHandler);

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

private function invokeHandler(e:InvokeEvent):void
{
   if (configService.hasConfiguration()) {
      .
      .
      .                
      var invokeRequest:InvokeRequest = InvokeManager.invokeManager.
          startupRequest;
                        
      if (invokeRequest.action == InvokeAction.PUSH) {
         .
         .
         .
      } else if (invokeRequest.action == InvokeAction.OPEN) {
           var pushSeqNum:int = invokeRequest.data.readInt();

           pushNotificationService.markPushAsRead(pushSeqNum);
                               
           var currentPush:Push = listContainer.findPushInList(pushSeqNum);
                               
           var updatedPush:Push = pushNotificationService.getPush(pushSeqNum);
           updatedPush.dateHeading = currentPush.dateHeading;
                               
           listContainer.updatePush(currentPush.dateHeading, updatedPush, 
              currentPush);
                               
           listContainer.selectItem(updatedPush);
                              
           PushRenderer.displayPushDialog(updatedPush);
     }
   }
}

Add a notification to the BlackBerry Hub

To add a notification for the sample application to the BlackBerry Hub, the sample application calls notifyNotification() and passes in a Notification object.

// Add a notification for the push to the BlackBerry Hub
var notification:Notification = new Notification();
notification.itemId = NOTIFICATION_PREFIX + push.seqNum;
notification.title = "Push Receiver";
notification.subTitle = "New " + push.fileExtension + " push received";
notification.invokeAction = "bb.action.OPEN";
notification.invokeTarget = INVOKE_TARGET_ID_OPEN;
notification.invokeMimeType = "text/plain";
                 
// We set the data of the invoke to be the seqnum of the 
// push so that we know which push needs to be opened
var openInvokeData:ByteArray = new ByteArray();
openInvokeData.writeInt(push.seqNum);
notification.invokeData = openInvokeData;
                 
NotificationManager.notificationManager.notifyNotification(notification);

Notice that notification.invokeAction matches the bb.action.OPEN action and notification.invokeMimeType matches the mime-type in the bar-descriptor.xml file. Additionally, the INVOKE_TARGET_ID_OPEN constant has a value of sample.pushreceiver.invoke.open which matches the invoke-target id in the bar-descriptor.xml file. For more information about the bar-descriptor.xml file, see Update your bar-descriptor.xml file.

Remove a notification from the BlackBerry Hub

To remove a notification for the sample application from the BlackBerry Hub, the sample application calls deleteNotification(), 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.

NotificationManager.notificationManager.deleteNotification(
   PushReceiver.NOTIFICATION_PREFIX + push.seqNum);

Remove all notifications from the BlackBerry Hub

To remove all the notifications for the sample application from the BlackBerry Hub, the sample application calls deleteNotification(). This code is called when the user taps either Mark All Open or Delete All in the sample application.

NotificationManager.notificationManager.deleteNotification();
comments powered by Disqus