Creating a PushService object

The sample application creates a PushService object in order to create a push session and perform push-related operations. In your application, you should create one global instance of the PushService object and associate it with one providerApplicationID and one invokeTargetID. If you try to create another PushService object with a different providerApplicationID or invokeTargetID, the Push Service throws an InitializationError.

providerApplicationID and invokeTargetID are parameters of the PushService() constructor. The providerApplicationID parameter corresponds to the application ID that you specified when you configured the sample application. For details about configuring the sample application, see Configure the sample application.

The invokeTargetID parameter corresponds to the value that's specified in the bar-descriptor.xml file. The invokeTargetID identifies the push-enabled application that's the target of a push message, and that's invoked when the push message arrives. For more information about updating your bar-descriptor.xml file, see Update your bar-descriptor.xml file.

The sample application must create a push session to establish communication with the PNS agent running on the device, and before it can use any of the other Push Service APIs. The application must create a session every time it starts. Since the call to create a session is asynchronous, the sample application registers event listeners before the call to create a session.

Call initializePushService()

The initializePushService() function initializes one shared static instance of net.rim.blackberry.push.PushService. All the service classes share this static instance of net.rim.blackberry.push.PushService. If the instance doesn't exist, getPushService() throws an error. The initializePushService() function also calls addEventHandlersToPushService() to register event listeners and event handlers for create session, create channel, destroy channel, register to launch, unregister from launch, and SIM card change events.

public function initializePushService():void
{
    var config:Configuration = configService.getConfiguration();
    .      
    .
    .
    previousApplicationId = config.providerApplicationId;
    try {
	     if (!config.providerApplicationId) {
	         pushService = new PushService(INVOKE_TARGET_ID);
         } else {
             pushService = new PushService(INVOKE_TARGET_ID, 
              config.providerApplicationId);
         }
	} catch (error:ArgumentError) {
        // Note: As a best practice in your application, you 
        // should handle this error gracefully.
    	trace("ArgumentError while creating push service: 
          " + error.message + "\r" + error.getStackTrace());
	    throw error;
	} catch(error:InitializationError) {
        // Note: Restarting the app or the device itself might
        // help, so we present this information to the user.
        trace("InitializationError while creating push service: 
          " + error.message + "\r" + error.getStackTrace());
					
        showInitializationErrorDialog();
	}
		
    addEventHandlersToPushService();     
    .
    .
    .
}  
		
private function addEventHandlersToPushService():void
{
	  getPushService().addEventListener(PushServiceErrorEvent.
           CREATE_SESSION_ERROR, errorEventHandler);
	  getPushService().addEventListener(PushServiceEvent.
           CREATE_SESSION_SUCCESS, successEventHandler);
	  getPushService().addEventListener(PushServiceErrorEvent.
           CREATE_CHANNEL_ERROR, errorEventHandler);
	  getPushService().addEventListener(
           CreateChannelSuccessEvent.CREATE_CHANNEL_SUCCESS, 
           createChannelSuccessHandler);
	  getPushService().addEventListener(PushServiceErrorEvent.
           DESTROY_CHANNEL_ERROR, errorEventHandler);
	  getPushService().addEventListener(PushServiceEvent.
           DESTROY_CHANNEL_SUCCESS, successEventHandler);
	  getPushService().addEventListener(PushServiceEvent.
           SIM_CHANGE, successEventHandler);
      getPushService().addEventListener(
           PushTransportReadyEvent.PUSH_TRANSPORT_READY, 
           pushTransportReadyHandler);
	  getPushService().addEventListener(PushServiceErrorEvent.
           REGISTER_TO_LAUNCH_ERROR, errorEventHandler);
	  getPushService().addEventListener(PushServiceEvent.
           REGISTER_TO_LAUNCH_SUCCESS, successEventHandler);
	  getPushService().addEventListener(PushServiceErrorEvent.
           UNREGISTER_FROM_LAUNCH_ERROR, errorEventHandler);
	  getPushService().addEventListener(PushServiceEvent.
           UNREGISTER_FROM_LAUNCH_SUCCESS, successEventHandler);
}
               
private function getPushService():PushService
{
    if (pushService) {
       return pushService;
    } else {
       trace("No existing instance of 
            net.rim.blackberry.push.PushService was 
            found to use.");
       throw(new Error("No existing instance of" + 
           "net.rim.blackberry.push.PushService was" + 
           "found to use."));
    }
}

Call createSession()

The sample application calls createSession() from the PushNotificationService interface to create a push session.

public function createSession():void
{
   // Initialize the PushService if it has not been already
   initializePushService();
	
   getPushService().createSession();
}
comments powered by Disqus