Running an app in the background

You can use the Push Service APIs to start running your app when a push message arrives, and to continue running it in the background. Using the Push Service saves valuable device resources because your app doesn’t have to be running all the time to receive push messages.

The sample app calls the launchApplicationOnPush() function with a value of true. This call registers the app so that it starts running in the background, if it isn't already running when a push message arrives. If you don't want to register your app so that it starts running in the background when a push message arrives, your app can call launchApplicationOnPush() with a value of false. The push messages that arrive when the value is false are delivered only if your app is running when the messages arrive.

PushCapture.prototype.successCreatePushService = function(service) {      
     // ...
     sample.pushcapture.pushService.launchApplicationOnPush(
         sample.pushcapture.launchapp, 
         sample.pushcapture.launchApplicationCallback);
 };

If the launch operation fails, the launchApplicationCallback() callback function shows the error handling and the error messages that you can use for debugging. In your app, you might want to handle the errors differently. For example, you might want to retry an operation first before displaying an error message. The API Reference lists the possible actions that you can take for each error constant. The API Reference also tells you which error codes apply to which function calls.

if (result == blackberry.push.PushService.SUCCESS) {
     // Successfully registered to launch
} else {             
     if (result == blackberry.push.PushService.INTERNAL_ERROR) {
         alert("Error: An internal error occurred while calling 
         launchApplicationOnPush. " + "Try restarting the 
         app.");
     } else if (result == blackberry.push.PushService.
                           CREATE_SESSION_NOT_DONE) {
         alert("Error: Called launchApplicationOnPush without an " +
         "existing session. It usually means a programming error.");
     } else {
         alert("Error: Received error code (" + result + ") after "
         + "calling launchApplicationOnPush.");            
     }
}

Last modified: 2015-06-11



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

comments powered by Disqus