用于获取令牌的 Phonegap Android 回调



>我正在使用 pushwoosh phonegap 示例,它工作正常。现在我需要通过回调检索令牌,但不知道如何做。在main.js中,我设置了一个变量来接收来自PushNotification的返回值.js函数initPushwoosh()

这是主要部分.js

function init() {
document.addEventListener("deviceready", deviceInfo, true);
var vvvtoken=document.addEventListener("deviceready", initPushwoosh, true);
var html='<h3>'+vvvtoken+' </h3>';
 $('#mailList').html(html).listview('refresh');
 }

这是来自 initPushwoosh() 的部分

function initPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
// CHANGE projectid & appid
pushNotification.registerDevice({ projectid: "xxxxxxxx", appid : "xxxxxxxxxxxx" },
function(status) {
var pushToken = status;
console.warn('push token: ' + pushToken);
return pushToken;
},
function(status) {
console.warn(JSON.stringify(['failed to register ', status]));
return "failed to register";
});
document.addEventListener('push-notification', function(event) {
var title = event.notification.title;
var userData = event.notification.userdata;
if(typeof(userData) != "undefined") {
console.warn('user data: ' + JSON.stringify(userData));
}
navigator.notification.alert(title);
});
}

我正在我的安卓手机上运行它,但我没有收到任何返回值。如何进行回调和返回令牌?

document.addEventListener 不会运行函数并返回结果。它所做的只是将函数设置为在将来的某个时间运行 - 几乎可以肯定是在您尝试显示令牌之后。

而不是

return pushtoken;

尝试:

$("h3").html(pushtoken);
return;

最新更新