2016-10-07 28 views
0

我有一個推送通知與GCM集成它工作正常。無論何時推送通知,當用戶在應用程序之外時,它都會重定向到特定頁面。但是,無論何時用戶在應用程序中,當通知到來時,它也會重定向特定頁面。所以我不想在用戶裏面重定向到特定的頁面。請幫我...只顯示推送通知當應用程序沒有運行在科爾多瓦android

這裏是我的代碼...對於推送通知

var pushNotification; 
 
function onDeviceReady() { 
 
    $("#app-status-ul").append('<li>deviceready event received</li>'); 
 
    document.addEventListener("backbutton", function (e) { 
 
     $("#app-status-ul").append('<li>backbutton event received</li>'); 
 
     if ($.mobile.activePage.is('#indexPage')) { 
 
      
 
      e.preventDefault(); 
 
      navigator.app.exitApp(); 
 
     } 
 
     else { 
 
      navigator.app.backHistory(); 
 
     } 
 
    }, false); 
 
    try { 
 
     pushNotification = window.plugins.pushNotification; 
 
     $("#app-status-ul").append('<li>registering ' + device.platform + '</li>'); 
 
     if (device.platform == 'android' || device.platform == 'Android') { 
 
      pushNotification.register(successHandler, errorHandler, { "senderID": "XXXXXXXXX", "ecb": "onNotification" }); \t \t // required! 
 
     } 
 
     //else { 
 
     // pushNotification.register(tokenHandler, errorHandler, { "badge": "true", "sound": "true", "alert": "true", "ecb": "onNotificationAPN" }); \t // 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('<li>EVENT -> RECEIVED:' + e.event + '</li>'); 
 
    switch (e.event) { 
 
     case 'registered': 
 
      if (e.regid.length > 0) { 
 
       $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>"); 
 
       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('<li>--INLINE NOTIFICATION--' + '</li>'); 
 
       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 { \t 
 
       if (e.coldstart) { 
 
        $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>'); 
 
        window.localStorage.setItem("push_que", e.payload.id); 
 
        window.localStorage.setItem("recordId",e.payload.recordId); 
 
       } 
 
       else 
 
        $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>'); 
 
       window.localStorage.setItem("push_que", e.payload.id); 
 
       window.localStorage.setItem("recordId",e.payload.recordId); 
 

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

這裏的onResume()方法,它有助於重定向特定頁面

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" && que_push != "trackComplaintTable" && que_push != "") { 
 
     window.localStorage.removeItem("push_que"); 
 
    } 
 

 
}

+0

什麼都會做功能pushNotify(); –

+0

在這個函數中,我打電話給Web服務,這是插入移動設備密鑰到數據庫 –

+0

#Homen我正在使用科爾多瓦pushPlugin –

回答

0

你只需要通過的onResume()函數在else塊,如果(e.foreground)語句** **裏面onNotification(E)功能

相關問題