简体   繁体   English

仅当应用程序未在Cordova android中运行时显示推送通知

[英]show push notification only when app is not running in cordova android

I have a push notification integrated with GCM It's working fine. 我有与GCM集成的推送通知,它工作正常。 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... 请帮我...

Here is my code...For Push Notification 这是我的代码...用于推送通知

 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" }); // 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('<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 { 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>'); } 

Here is onResume() method which helps to redirect specific page 这是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"); } } 

您只需要在onNotification(e)函数的if (e.foreground)语句**的 else块中传递onResume( 函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序在Cordova中运行时,推送通知未显示 - Push notification not showing when app is in running in cordova 在状态栏Android Cordova中推送通知 - Push Notification in status bar Android Cordova 创建网络推送通知(不是用于Android应用程序,而是仅用于网站)? - create web push notification(not for android app, but for website only)? Android在Mac OS上运行Meteor App时的Cordova目录错误 - Cordova directory error when Android running Meteor App on Mac OS 在Android TV上运行Cordova应用程序时如何调试JavaScript错误 - How to debug JavaScript errors when running a Cordova App on Android TV 在发布模式下运行Android Cordova应用程序时,Ajax调用失败 - Ajax calls fail when running Android Cordova app in Release mode 如何使用Quickblox在Cordova应用程序中实现推送通知支持? - How to implement push notification support in Cordova app using Quickblox? 引用错误onnotification未在推送通知cordova android中定义 - Reference error onnotification is not defined in push notification cordova android 下载文件并在下载应用程序或cordova应用程序的通知中显示 - Download a file and show it in downloads app or in notification in a cordova app cordova push + angular-在收到通知时实施逻辑 - cordova push + angular - implement logic on when notification recieved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM