Handling notifications in the BlackBerry Hub

Our 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, our UI application receives an open invoke event, and displays the push message that corresponds to the notification.
  • When our headless application receives a push message, it adds a notification to the BlackBerry Hub.
  • When a user taps on the push message in our UI application, our UI application removes the notification from the BlackBerry Hub.
  • When a user deletes an individual push message in our UI application, our UI application removes the notification for the push message from the BlackBerry Hub.
  • When a user taps Mark All Open or Delete All in our UI application, our UI application removes all the notifications for our application from the BlackBerry Hub.

Display a push message for an open invoke event

Our UI application connects a slot to the InvokeManager invoked signal to receive invoke requests. An open invoke request occurs when a user taps a notification for a push message in the BlackBerry Hub. It's essential that we connect to the InvokeManager signal at the beginning of the application. If we don't, our application might not receive the signal.

// Connect the InvokeManager signals and slots.

checkConnectResult(connect(m_invokeManager, SIGNAL(invoked(const 
  bb::system::InvokeRequest&)), SLOT(onInvoked(const 
  bb::system::InvokeRequest&))));

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

void App::onInvoked(const InvokeRequest &request)
{
	if (request.action().compare(BB_OPEN_INVOCATION_ACTION) == 0){
		qDebug() << "Received open action";
		// Received an invoke request to open an existing push 
        // (ie. from a notification in the BlackBerry Hub).
		// The payload from the open invoke is the seqnum for 
        // the push in the database
		openPush(request.data().toInt());
	}
}

Add a notification to the BlackBerry Hub

To add a notification for our sample application to the BlackBerry Hub, our headless application creates a Notification object, adds an InvokeRequest, and calls notify().

// Create a notification for the push that will be added to the 
// BlackBerry Hub.
    Notification *notification = new Notification(NOTIFICATION_PREFIX 
      + QString::number(push.seqNum()),this);
    notification->setTitle("Push Collector");
    notification->setBody(QString("New %0 push received").arg(
      push.fileExtension()));

    // Add an invoke request to the notification
    // This invoke will contain the seqnum of the push.
    // When the notification in the BlackBerry Hub is selected, this  
    // seqnum will be used to lookup the push in the database
    // and display it.
    InvokeRequest invokeRequest;
    invokeRequest.setTarget(INVOKE_TARGET_KEY_OPEN);
    invokeRequest.setAction(BB_OPEN_INVOCATION_ACTION);
    invokeRequest.setMimeType("text/plain");
    invokeRequest.setData(QByteArray::number(push.seqNum()));
    notification->setInvokeRequest(invokeRequest);

    // Add the notification for the push to the BlackBerry Hub
    // Calling this method will add a "splat" to the application icon, 
    // indicating that a new push has been received
    notification->notify();

Notice that the InvokeRequest action matches the bb.action.OPEN action and InvokeRequest MimeType matches the mime-type in the bar-descriptor.xml file. Additionally, the INVOKE_TARGET_KEY_OPEN constant has a value of com.example.pushCollector.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 our sample application from the BlackBerry Hub, our application calls deleteFromInbox(), 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 our application, and answers Yes to the confirmation message. Our application also deletes the push message from our application.

Notification::deleteFromInbox(NOTIFICATION_PREFIX + QString::number(
    push.seqNum()));

Remove all notifications from the BlackBerry Hub

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

Notification::deleteAllFromInbox();

Last modified: 2013-12-21

comments powered by Disqus