Receiving a push message

Our sample application is divided into two parts: a UI application and a headless application. Both use their own instance of InvokeManager, and they each listen for different types of invoke events. It's essential that the UI application and headless application connect to the InvokeManager signal at the beginning of the application. If they don't, the applications might not receive the signal.

The invoke request can be a push invoke request or an open invoke request. A push invoke request is processed by the headless application, and occurs when the application receives a push message, a start event, or a command from the UI application. An open invoke request is processed by the UI application, and occurs when a user taps a notification for a push message in the BlackBerry Hub. The following code sample shows you how our headless application handles a push invoke request. To see how our UI application handles an open invoke request, see Handling notifications in the BlackBerry Hub.

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

The onInvoked() function first checks the action property of InvokeRequest. If the property matches the string constant BB_PUSH_INVOCATION_ACTION, the function then checks to see that the bb::network::PushService instance is created, and that the application ID is stored. If both are true, the headless application extracts the push message from the invoke request by passing the invoke request into the PushPayload constructor. The pushNotificationHandler() function then processes the push message. In your application, you can provide the code to process the push message in a way that's appropriate for your environment.

If the action property matches the string constant BB_PUSH_COLLECTOR_COMMAND_ACTION, this means that the UI application sent a command to the headless application. The headless application extracts the command from the invoke request by decoding the JSON data into a QVariantList. The commandMessageHandler() function then processes the command. The command object that is passed from the UI application to the headless application is specific to our application, and shows you one way to communicate between UI and headless applications. In your application, you can use a communication mechanism that's appropriate for your application.

void AppHeadless::onInvoked(const InvokeRequest& request)
{
  if (request.action().compare(BB_PUSH_INVOCATION_ACTION) == 0) {
    if (m_configurationService.hasConfiguration()) {
  	 // The underlying PushService instance might not have been
  	 // initialized when an invoke first comes in.
  	 // Make sure that we initialize it here if it hasn't been
       // already. It requires an application ID (for consumer 
  	 // applications) so we have to check that configuration 
       // settings have already been stored.
  	 m_pushNotificationService->initializePushService();

	  	qDebug() << "Received push action";

	  	// Received an incoming push.
	  	// Extract it from the invoke request and then process 
          // it.
		  PushPayload payload(request);
		  if (payload.isValid()) {
		  	pushNotificationHandler(payload);
		  }
		}
	} else if (request.action().
          compare(BB_PUSH_COLLECTOR_COMMAND_ACTION) == 0){
		qDebug() << "Received command action" ;
		QVariantList commandData = m_jsonDA->
          loadFromBuffer(request.data()).toList();
		commandMessageHandler(commandData);
	}
}

Last modified: 2013-12-21

comments powered by Disqus