Handling a connection error

There might be rare occasions when the sample application loses its connection to the PNS agent running on the device. The PNS agent maintains a connection between the PPG and the application, and forwards push notifications from the PPG to the appropriate application instance.

When the application loses the connection, it can't perform these operations: create a session, create or destroy a channel, register to start in the background when a push message arrives, or unregister from starting in the background. When these operations fail because of the lost connection, the Push Service issues a PUSH_SERVICE_CONNECTION_CLOSED error code.

To handle this error, the sample application registers an event listener that listens for the PushServiceConnectionReadyEvent. When the application re-establishes communication with the PNS agent, the application triggers the PushServiceConnectionReadyEvent.

// Listen for a push service connection ready event (and handle it)
    PUSH_SERVICE_CONNECTION_READY, pushServiceConnectionReady);

The pushServiceConnectionReady event handler checks the value of e.lastFailedOperation to determine which operation failed. The event handler then displays a message indicating that the connection is restored, and what action to perform to complete the recovery from the connection error.

private function pushServiceConnectionReady(e:PushServiceConnectionReadyEvent):
   var pushServiceConnectionReadyDialog:AlertDialog = new AlertDialog();
   pushServiceConnectionReadyDialog.title = "Push Receiver";
   message:String = "The Push Service connection is now available. Please try ";
   if ((e.lastFailedOperation == PushServiceConnectionReadyEvent.
             CREATE_SESSION) || 
          (e.lastFailedOperation == PushServiceConnectionReadyEvent.
             REGISTER_TO_LAUNCH) || 
          (e.lastFailedOperation == PushServiceConnectionReadyEvent.
             UNREGISTER_FROM_LAUNCH))  {
           message += "to save your configuration ";
   } else if (e.lastFailedOperation == PushServiceConnectionReadyEvent.
             CREATE_CHANNEL) {
           message += "registering ";
   } else if (e.lastFailedOperation == PushServiceConnectionReadyEvent.
             DESTROY_CHANNEL) {
          message += "unregistering ";
   } else {
          message += "your last command ";
   message += "again.";
   pushServiceConnectionReadyDialog.message = message;

The sample application handles the connection error by explicitly telling you what actions to try to recover from the error. In your application, we recommend that your event handler listen for the PushServiceConnectionReadyEvent, check e.lastFailedOperation for the operation that failed, and then try the operation again.