Handling a push transport error or PPG server error

There might be occasions when the sample application can't create or destroy a channel because of a push transport error or a PPG server error. A push transport error occurs when there's a problem with the mobile network, the user's wireless connection, or the low-level communications layer that the Push Service uses. A PPG server error occurs when the PPG server is unavailable and returns an error. These errors occur only if the PPG is the BlackBerry Internet Service.

When the channel operations fail because of one of these errors, the Push Service issues a PushServiceErrorEvent.PUSH_TRANSPORT_UNAVAILABLE or PushServiceErrorEvent.PPG_SERVER_ERROR error code. The sample application displays an error message to the user when it receives one of these error codes.

private function createChannelError(e:PushServiceErrorEvent):void
  {
    var message:String;
    if (e.errorID == PushServiceErrorEvent.PUSH_TRANSPORT_UNAVAILABLE) {
          message = "Create channel failed as the push transport is 
          unavailable. " + "Verify your mobile network and/or Wi-Fi 
          are turned on. " + "If they are on, you will be notified when 
          the push transport is available again.";

    } else if (e.errorID == PushServiceErrorEvent.PPG_SERVER_ERROR) {
         message = "Create channel failed as the PPG is currently 
         returning a server error. " + "You will be notified when the 
         PPG is available again.";                           

    } else {
         // Typically in your own application you wouldn't want to 
         // dipslay this error to your users
         message = "Create channel failed with error code: " 
             + e.errorID + ".";     
    }
                 
    dialogError("Register", message, e.text);
  }

To handle these errors, the sample application registers an event listener that listens for the PushTransportReadyEvent so that the application can try to create or destroy the channel again.

// Listen for a push transport ready event (and handle it)         
pushNotificationService.addEventListener(PushTransportReadyEvent.
    PUSH_TRANSPORT_READY, pushTransportReady);

The pushTransportReady event handler checks the value of lastFailedOperation to determine which operation failed. The event handler also displays a message to the user to register again if the create channel operation failed, or to unregister again if the destroy channel operation failed. The user needs to register or unregister again because the errors prevented the user from successfully registering or unregistering in the first place. For details about registering or unregistering the user, see Creating a push channel or Destroying a push channel.

private function pushTransportReady(e:PushTransportReadyEvent):void
{
  var pushTransportReadyDialog:AlertDialog = new AlertDialog();
  pushTransportReadyDialog.title = "Push Receiver";
  pushTransportReadyDialog.addButton("Ok");
                     
  var message:String = "The push transport/wireless network/PPG is now 
      available. Please try ";
                    
  if (e.lastFailedOperation == PushTransportReadyEvent.CREATE_CHANNEL) {
        message += "registering ";
  } else {
          message += "unregistering ";
         }
                     
         message += "again.";
                     
         pushTransportReadyDialog.message = message;
         pushTransportReadyDialog.show();
}

The sample application handles the errors by explicitly telling the user about the errors. In your application, you could handle the errors the same way, or your event handler could listen for the PushTransportReadyEvent, check lastFailedOperation for the operation that failed, and then try the operation again without telling the user about the errors.

comments powered by Disqus