Exiting after processing a push message

If you register your application to start running in the background when a push message arrives using the launchApplicationOnPush() 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.

 * Boolean indicating whether the application has ever been in the 
 * foreground. This boolean is used to determine whether we are 
 * dealing with the situation where the application is set to launch 
 * on a new push, the application is currently not running, and a new 
 * push comes in. The application will be launched in the background 
 * to process the push. If the user does not bring the app to the 
 * foreground while we are processing the push, then we exit 
 * the application. This is to save battery life and performance on 
 * the device by not having the application running in the background 
 * after every push that comes in.
this.hasBeenInForeground = false;

The sample application checks the current window state when it starts to see if it starts full screen. If it does, the application sets the hasBeenInForeground variable to true. The application also listens for the resume event. If this event occurs, the application was displayed full screen by the user, and the hasBeenInForeground variable is set to true.

PushCapture.prototype.initPushService = function() {
    // Check if the application started up in the foreground.
    // We are trying to determine if the application was launched in
    // the background and so we would be able to process a push and 
    // exit from the application.
    if (blackberry.app.windowState == "fullscreen") {
        sample.pushcapture.hasBeenInForeground = true;

    // We also use the resume event to help determine if the 
    // application has ever been brought to the foreground 
    // (from the background - hence it's being "resumed").
    blackberry.event.addEventListener("resume", sample.pushcapture.onResume);


PushCapture.prototype.onResume = function() {
   sample.pushcapture.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.

// Exit the application after processing the push if the              
// application has not been brought to the foreground              
if (!sample.pushcapture.hasBeenInForeground) {           
    setTimeout(function() {
    }, 1000);              

Last modified: 2014-03-10

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus