Exiting after processing a push message

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

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

this.hasBeenInForeground = false;

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

PushCapture.prototype.initPushService = function() {
    // Check whether the app started up in the foreground.
    // If it was launched in the background, the app can process 
    // a push and then exit.
    if (blackberry.app.windowState == "fullscreen") {
        sample.pushcapture.hasBeenInForeground = true;

    // The app listens for the resume event to determine if the 
    // app was previously brought to the foreground.  
    document.addEventListener("resume", sample.pushcapture.onResume);
    // More code


PushCapture.prototype.onResume = function() {
   sample.pushcapture.hasBeenInForeground = true;

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

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

Last modified: 2015-06-11

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

comments powered by Disqus