我在使用Firebase Cordova插件时遇到了问题。
当我的应用程序运行index.html文件时,我可以收到通知。但是当window.location.href
被改变时,通知不再起作用。我做错了什么?
在index.js文件中的onDeviceReady
函数:
onDeviceReady: function() {
if(window.localStorage.getItem("token") == null)
{window.localStorage.setItem("token", "")};
setTimeout(getTheToken, 1000);
function getTheToken() {
if(window.localStorage.getItem("token") == ""){
FCMPlugin.getToken(
function (token) {
if (token == null) {
console.log("null token");
setTimeout(getTheToken, 1000);
navigator.notification.alert("Bucle"); // Bucle === Loop
} else {
console.log("I got the token: " + token);
window.localStorage.setItem("token", token);
alert("Ya tengo Token" + token);
}
},
function (err) {
alert('error retrieving token: ' + err);
}
);
}
}
FCMPlugin.onNotification(
function(data){
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert("Tapped: " + JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert("Not tapped: " + JSON.stringify(data) );
}
},
function(msg){
alert('onNotification callback successfully registered: ' + msg);
console.log('onNotification callback successfully registered: ' + msg);
},
function(err){
alert('Error registering onNotification callback: ' + err);
console.log('Error registering onNotification callback: ' + err);
}
);
app.receivedEvent('deviceready');
我有另一个JS文件:
$(document).ready(function(){
$("#entrar").on("click",function(e){
var identificador = $("#asociado_id").val();
var token = window.localStorage.getItem("token");
login(identificador, token, "#entrar");
});
});
以上代码中,如果identificador
存在,则登录功能将重定向到另一个页面。
success: function(respuesta) {
if(respuesta.tk == "true"){
window.localStorage.setItem("login", "true");
window.location.href = "main.html";
在重定向页面中,通知未到达。该页面如下所示:
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script>
<!-- <script type="text/javascript" src="cordova.js"></script>-->
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/velocity.min.js"></script>
<script type="text/javascript" src="js/main_menu.js"></script> <!-- Resource jQuery -->
如果页面中激活了
<script cordova.js>
,应用程序将进入循环(bucle)。