Exiting after processing a push message

If you register your application to start running in the background when a push message arrives using the registerToLaunch() function, your application continues to run after it processes the message. This can use up device resources, so it's a good practice to exit your application after it processes a push message. You have to be careful, however, not to exit your application if it was ever displayed full screen (in the foreground) because the user might still be using your application.

To exit after processing a push message, the sample application creates the hasBeenInForeground Boolean variable to check whether the application was ever displayed full screen. The variable is set initially to false.

// Whether or not the application has at some point in time been 
// running in the foreground
private static var hasBeenInForeground:Boolean = false;

The sample application listens for the ACTIVATE event. If this event occurs, the application was displayed full screen, and the hasBeenInForeground variable is set to true.

NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, 
     handleActivate);

     private function handleActivate(e:Event):void
          {
               hasBeenInForeground = true;
          }

After the sample application processes the push message, it checks the hasBeenInForeground variable. If the variable is true, the application doesn't exit. If the variable is false, the application was never displayed full screen. The application started in the background when a push message arrived, and can exit after it processes the message.

private function pushNotificationHandler(pushPayload:PushPayload):void
     {
     .
     .
     .
     if (!hasBeenInForeground) {
          NativeApplication.nativeApplication.exit();
          }
     }
comments powered by Disqus