Receiving a push message

The sample application registers an event listener that listens for invoke events. The invoke event can be a push invoke event or an open invoke event. A push invoke event occurs when the application receives a push message. An open invoke event occurs when a user taps a notification for a push message in the BlackBerry Hub. The following code sample shows you how the sample application handles a push invoke event. To see how the sample application handles an open invoke event, see Handling notifications in the BlackBerry Hub.

This is the event listener that the sample application registers.

// Add an event listener to handle incoming invokes 
InvokeManager.invokeManager.addEventListener(InvokeEvent.INVOKE, 
   invokeHandler);

The invokeHandler() function first checks to see that the net.rim.blackberry.push.PushService instance is created, and that the application ID is stored.

private function invokeHandler(e:InvokeEvent):void
{			
   if (configService.hasConfiguration()) {
    // The underling net.rim.blackberry.push.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.
	pushNotificationService.initializePushService();
				
	var invokeRequest:InvokeRequest = InvokeManager.invokeManager.
         startupRequest;
		
	if (invokeRequest.action == InvokeAction.PUSH) {
    	var pushPayload:PushPayload = pushNotificationService.
            extractPushPayload(invokeRequest);
				
		pushNotificationHandler(pushPayload);
    } else if (invokeRequest.action == InvokeAction.OPEN) { 
      .     
      .
      .
    }
  }
}

The function then checks the action property of the invokeRequest to see if it matches the string constant InvokeAction.PUSH. If the property matches the constant, the application extracts the push message from the invoke request using extractPushPayload(). 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.

comments powered by Disqus