gpt4 book ai didi

javascript - Firebase 云消息传递插件无法正常工作重定向的 HTML 页面

转载 作者:行者123 更新时间:2023-12-01 01:41:17 24 4
gpt4 key购买 nike

我的 Firebase Cordova 插件有问题。

当我的应用程序运行文件index.html 时,我会收到通知。但是当 window.location.href更改后,通知不起作用。我做错了什么?

onDeviceReady文件index.js中的函数:

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)。

最佳答案

代码顺序似乎有问题

尝试将 FCMPlugin.onNotification(...); 移至 FCMPlugin.getToken(...);

之后

这是我的 index.js 文件,当我希望我的应用程序具有带有 FCM 的推送通知时,我会使用该文件。希望对您有帮助。

var app = {

initialize: function () {
this.bindEvents();
},

bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener('backbutton', this.onBackKeyDown, false);
},
onDeviceReady: function () {

app.notificacionesPush();


},

notificacionesPush: function () {

FCMPlugin.getToken(
function (token) {

var obj = {"token": token};

Ajax("http://MI-URL/webservice.php", obj, function (data) {
if (data.code === "OK")
console.info("Token de Dispositivo registrado correctamente");
else
console.info("Error en registro de Token");

}, function () {
console.warn("Error en registro de Token");
});
},
function (err) {
console.warn('error retrieving token: ' + err);
}
);


FCMPlugin.onNotification(
function (data) {
if (data.wasTapped) {
console.info(JSON.stringify(data));
} else {
console.info(JSON.stringify(data));
}
},
function (msg) {
console.info('onNotification callback successfully registered: ' + msg);
},
function (err) {
console.info('Error registering onNotification callback: ' + err);
}
);
},}; app.initialize();

关于javascript - Firebase 云消息传递插件无法正常工作重定向的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41448514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com