Destroying a push channel

The sample application destroys the push channel to the PPG to stop receiving push messages.

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 unsubscribing. The sample application provides an Unregister 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 Unregister dialog box by tapping Unregister at the bottom of the device screen. The Unregister dialog box appears.

Unregister dialog box where you type a username and password.

After you tap Unregister in the dialog box, the sample application tries to destroy the channel.

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

Register event listeners and handlers

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

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

getPushService().addEventListener(PushServiceEvent.
   DESTROY_CHANNEL_SUCCESS, successEventHandler);

Call destroyChannel()

The sample application calls destroyChannel() from the PushNotificationService interface to destroy a channel.

public function destroyChannel():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();
                
     getPushService().destroyChannel();
 }

Call unsubscribeFromPushInitiator()

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

public function unsubscribeFromPushInitiator(user:User):void
 {
   new UnregisterHandler(this).unsubscribeFromPushInitiator(user); 
 }

The UnregisterHandler object handles the unsubscribe from the Push Initiator.

public function unsubscribeFromPushInitiator(user:User):void
{
    hasPushInitiatorErrorAlreadyBeenDispatched = false;
			
    // Keep track of the current user's information
    // If it matches the one stored in the database, then the 
    // one in the database will be removed 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 unsubscribeUrl:String = config.pushInitiatorUrl 
       + "/unsubscribe";
			
    var request:URLRequest = new URLRequest(unsubscribeUrl);
			
    var requestVars:URLVariables = new URLVariables();
    requestVars.appid = escape(config.providerApplicationId);
    requestVars.username = escape(user.userId);
    requestVars.password = escape(user.password);
			
    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 unsubscribe servlet of the Push Service SDK
    urlLoader.addEventListener(Event.COMPLETE, 
       loaderCompleteHandler, false, 0, true);
			
   // Add event listeners for handling errors back from the 
   // unsubscribe 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:UnsubscribeFromPushInitiatorErrorEvent = 
            new UnsubscribeFromPushInitiatorErrorEvent
            (UnsubscribeFromPushInitiatorErrorEvent.
            UNSUBSCRIBE_FROM_PI_ERROR, false, false, e.message, 
            e.errorID);				
        eventDispatcher.dispatchEvent(errorEvent); 	
    }
}
comments powered by Disqus