Creating a push channel

The sample application creates a push channel to the PPG to receive push messages. The application only needs to create a channel the first time it starts or if a destroy channel occurred previously and you want to start receiving push messages again.

If you chose to subscribe with the Push Service SDK when you configured the sample application, the Push Initiator requires a username and password that it authenticates before allowing the subscription. The sample application provides a Register dialog box where the user can type any username and password. For your application, check with your push content provider to get a valid username and password.

You can open the Register dialog box by tapping Register at the bottom of the device screen. The Register dialog box appears.

Register dialog box where you type a username and password.

After you type the username and password, and tap Register in the dialog box, the sample application tries to create a channel.

If you're not subscribing with the Push Service SDK, you don't need to provide a username and password. You can simply tap Register at the bottom of the device screen, and the sample application tries to create a channel.

Register event listeners and event handlers

Since the call to create a channel is asynchronous, the sample application registers the following event listeners and event handlers. When the create channel event occurs, the application calls the corresponding event handlers.

getPushService().addEventListener(PushServiceErrorEvent.
     CREATE_CHANNEL_ERROR, errorEventHandler;

getPushService().addEventListener(CreateChannelSuccessEvent.
     CREATE_CHANNEL_SUCCESS, successEventHandler);

Call createChannel()

The sample application calls createChannel() from the PushNotificationService interface to create a channel, and passes in the PPG URL in the parameter list that you specified when you configured the sample application. If you're using the BlackBerry Device Service as the PPG, the PPG URL is a null value. If the channel creation is successful, createChannelSuccessHandler() returns a token that represents the address of the device. The Push Initiator uses the token to send push messages to the device. If the channel creation is unsuccessful, the sample application throws an error.

public function createChannel():void
{
  // This should not be null, since we require config 
  // settings to be present
  // before a user can register/unregister
  var config:Configuration = configService.getConfiguration();
	
  try {
    // For a consumer application, config.ppgUrl will 
    // be a non-null value. For an enterprise application, 
    // config.ppgUrl will be null.
	getPushService().createChannel(config.ppgUrl);		
  } catch (error:ArgumentError) {
	trace("ArgumentError while creating channel: " +
      error.message + ".\r" + error.getStackTrace());
	throw error;
  }		
}

Call subscribeToPushInitiator()

If the channel creation is successful, the sample application calls subscribeToPushInitiator() from the PushNotificationService interface to subscribe with the Push Initiator, and passes off the call to the RegisterHandler object. The subscribeToPushInitiator() parameter list includes the token to the Push Initiator and the username and password that you typed in the Register dialog box. If you're not subscribing with the Push Service SDK, the sample application doesn't call subscribeToPushInitiator().

public function subscribeToPushInitiator(user:User, token:String):void
{
   new RegisterHandler(this).subscribeToPushInitiator(user, token);
}

The RegisterHandler object handles the subscription with the Push Initiator.

public function subscribeToPushInitiator(user:User, token:String):void 
{
    hasPushInitiatorErrorAlreadyBeenDispatched = false;
			
    // Keep track of the current user's information so it 
    // can be stored later on a success
    currentUser = user;
			
    // This should not be null, since we require config 
    // settings to be 
    // present before a user can register/unregister
    var config:Configuration = configurationDAO.getConfiguration();
			
    var subscribeUrl:String = config.pushInitiatorUrl 
          + "/subscribe";
			
    var request:URLRequest = new URLRequest(subscribeUrl);
			
    var requestVars:URLVariables = new URLVariables();
    requestVars.appid = escape(config.providerApplicationId);
    requestVars.address = token;
    requestVars.osversion = Device.device.scmBundle;
    requestVars.model = Device.device.hardwareID;
    requestVars.username = escape(user.userId);
    requestVars.password = escape(user.password);

    if (config.usingPublicPushProxyGateway) {
       	requestVars.type = "public";
    } else {
       	requestVars.type = "bds";
    }
			
    request.data = requestVars;
    request.method = URLRequestMethod.GET;
			
    var urlLoader:URLLoader = new URLLoader();
    urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			
    // Add an event listener for a successful HTTP response 
    // back from the subscribe servlet of the Push Service SDK
    urlLoader.addEventListener(Event.COMPLETE, 
       loaderCompleteHandler, false, 0, true);
			
   // Add event listeners for handling errors back from the 
   // subscribe servlet of the Push Service SDK
   urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, 
      httpStatusHandler, false, 3, true);
   urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, 
      securityErrorHandler, false, 2, true);
   urlLoader.addEventListener(IOErrorEvent.IO_ERROR, 
      ioErrorHandler, false, 1, true);
			
    try {
        urlLoader.load(request);
    } catch(e:Error) {
        hasPushInitiatorErrorAlreadyBeenDispatched = true;
				
	    var errorEvent:SubscribeToPushInitiatorErrorEvent = 
          new SubscribeToPushInitiatorErrorEvent(
          SubscribeToPushInitiatorErrorEvent.
          SUBSCRIBE_TO_PI_ERROR, false, false, e.message, 
          e.errorID);			
        eventDispatcher.dispatchEvent(errorEvent); 	
    }
}
comments powered by Disqus