2012-12-02 18 views
0

我使用pushwoosh phonegap示例,它工作正常。現在我需要用回調檢索令牌,但不知道如何去做。 在main.js,我設置一個變量來接收來自PushNotification.js功能initPushwoosh()的返回值Phonegap Android回撥獲得令牌

這裏是在main.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); 
}); 

} 

我在我的android手機上運行它,我沒有收到任何返回的值。如何使回調和返回令牌?

回答

0

document.addEventListener不運行該函數並返回結果。它所做的就是設置函數在將來的某個時間運行 - 幾乎可以肯定地在你嘗試顯示令牌之後。

而不是

return pushtoken; 

嘗試:

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

我試了一下。它什麼都不顯示。我們可以這樣使用它:document.addEventListener(「deviceready」,var vvvtoken = initPushwoosh,true); – Elie