show push notification only when app is not running in cordova android
<p>I have a push notification integrated with GCM It's working fine. Whenever push notification comes then it redirected to specific page when user outside the application. But whenever user inside the application then it also redirect the specific  page when notification come. So i don't want to redirect to specific page when user inside the. Please help me...</p>

<p>Here is my code...For Push Notification</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>var pushNotification;
function onDeviceReady() {
    $("#app-status-ul").append('&lt;li&gt;deviceready event received&lt;/li&gt;');
    document.addEventListener("backbutton", function (e) {
        $("#app-status-ul").append('&lt;li&gt;backbutton event received&lt;/li&gt;');
        if ($.mobile.activePage.is('#indexPage')) {
            
            e.preventDefault();
            navigator.app.exitApp();
        }
        else {
            navigator.app.backHistory();
        }
    }, false);
    try {
         pushNotification = window.plugins.pushNotification;
        $("#app-status-ul").append('&lt;li&gt;registering ' + device.platform + '&lt;/li&gt;');
        if (device.platform == 'android' || device.platform == 'Android') {
            pushNotification.register(successHandler, errorHandler, { "senderID": "XXXXXXXXX", "ecb": "onNotification" });  // required!
        }
        //else {
        //    pushNotification.register(tokenHandler, errorHandler, { "badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN" }); // required!
        //}
    }
    catch (err) {
        txt = "There was an error on this page.\n\n";
        txt += "Error description: " + err.message + "\n\n";
        alert(txt);
    }
}
// handle GCM notifications for Android
function onNotification(e) {
    $("#app-status-ul").append('&lt;li&gt;EVENT -&gt; RECEIVED:' + e.event + '&lt;/li&gt;');
    switch (e.event) {
        case 'registered':
            if (e.regid.length &gt; 0) {
                $("#app-status-ul").append('&lt;li&gt;REGISTERED -&gt; REGID:' + e.regid + "&lt;/li&gt;");
                localStorage.setItem("REGID", e.regid);
                console.log("regID = " + e.regid);
            }
            break;
        case 'message':
            // if this flag is set, this notification happened while we were in the foreground.
            // you might want to play a sound to get the user's attention, throw up a dialog, etc.
            if (e.foreground) {
                $("#app-status-ul").append('&lt;li&gt;--INLINE NOTIFICATION--' + '&lt;/li&gt;');
                window.localStorage.setItem("push_que", e.payload.id);
                window.localStorage.setItem("recordId",e.payload.recordId);
                var push_que = e.payload.id;
                var soundfile = e.soundname || e.payload.sound;
                var my_media = new Media("/android_asset/www/" + soundfile);
                my_media.play();
            }
            else { 
                if (e.coldstart) {
                    $("#app-status-ul").append('&lt;li&gt;--COLDSTART NOTIFICATION--' + '&lt;/li&gt;');
                    window.localStorage.setItem("push_que", e.payload.id);
                    window.localStorage.setItem("recordId",e.payload.recordId);
                }
                else
                    $("#app-status-ul").append('&lt;li&gt;--BACKGROUND NOTIFICATION--' + '&lt;/li&gt;');
                window.localStorage.setItem("push_que", e.payload.id);
                window.localStorage.setItem("recordId",e.payload.recordId);

            }
            $("#app-status-ul").append('&lt;li&gt;MESSAGE -&gt; MSG: ' + e.payload.message + '&lt;/li&gt;');
          
            //android only
            $("#app-status-ul").append('&lt;li&gt;MESSAGE -&gt; MSGCNT: ' + e.payload.msgcnt + '&lt;/li&gt;');
            //amazon-fireos only
            $("#app-status-ul").append('&lt;li&gt;MESSAGE -&gt; TIMESTAMP: ' + e.payload.timeStamp + '&lt;/li&gt;');
            onResume();
            break;
        case 'error':
            $("#app-status-ul").append('&lt;li&gt;ERROR -&gt; MSG:' + e.msg + '&lt;/li&gt;');
            break;
        default:
            $("#app-status-ul").append('&lt;li&gt;EVENT -&gt; Unknown, an event was received and we do not know what it is&lt;/li&gt;');
            break;
    }
    pushNotify();
}
function successHandler(result) {
    $("#app-status-ul").append('&lt;li&gt;success:' + result + '&lt;/li&gt;');
}
function errorHandler(error) {
    $("#app-status-ul").append('&lt;li&gt;error:' + error + '&lt;/li&gt;');
}</code></pre>
</div>
</div>
</p>

<p>Here is onResume() method which helps to redirect specific page</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>function onResume() {
    //toast('App resumed', 'short');

    var que_push = window.localStorage.getItem("push_que");
    if (que_push == "inboxDetails") {
        $.mobile.changePage("#inboxDetails");
        window.localStorage.removeItem("push_que");
    }
    if (que_push == "trackComplaintTable") {
        var recordId = window.localStorage.getItem("recordId");
        showtrackcomplaintdetail(recordId);
        $.mobile.changePage("#trackComplaintTable");
        window.localStorage.removeItem("push_que");
    }
    // if a number is sent  open a specific news article calling a specific funcion that loads the "articles"
    if (que_push != "inboxDetails" &amp;&amp; que_push != "trackComplaintTable" &amp;&amp; que_push != "") {
        window.localStorage.removeItem("push_que");
    }

}</code></pre>
</div>
</div>
</p>