Phonegap/Pushwoosh Android检索设备id/令牌
如何在设备注册时检索设备id/令牌?我使用Phonegap Pushwoosh示例,效果很好。但是我不知道如何在设备注册initPushwoosh检索令牌 我不是一个专业的程序员。任何帮助都将不胜感激 我有一个index.html,可以初始化Phonegap/Pushwoosh Android检索设备id/令牌,android,cordova,push-notification,token,Android,Cordova,Push Notification,Token,如何在设备注册时检索设备id/令牌?我使用Phonegap Pushwoosh示例,效果很好。但是我不知道如何在设备注册initPushwoosh检索令牌 我不是一个专业的程序员。任何帮助都将不胜感激 我有一个index.html,可以初始化 在main.js中 function init() { document.addEventListener("deviceready", deviceInfo, true); document.addEventListener("devicer
在main.js中
function init() {
document.addEventListener("deviceready", deviceInfo, true);
document.addEventListener("deviceready", initPushwoosh, true);
}
在PushNotification.js中
function initPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
// CHANGE projectid & appid
pushNotification.registerDevice({ projectid: "xxxxxxx", appid : "xxxxxxxx" },
function(status) {
var pushToken = status;
console.warn('push token: ' + pushToken);
},
function(status) {
console.warn(JSON.stringify(['failed to register ', status]));
});
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);
});
}
第一部分是.registerDevice,令牌可能是pushToken,但我不知道如何从这个函数中检索它!
最好是将其发送到MySQL数据库,让我们称之为smartphonedb.tokentable
我修改了initPushwoosh()以使用Ajax将令牌发送给MySQL(见下文),我在MySQL上没有收到任何消息。我是否发送了正确的令牌参数(pushToken)
函数initPushwoosh()
{
var pushNotification=window.plugins.pushNotification;
//更改projectd和appid
pushNotification.registerDevice({projectid:“xxxxxx”,appid:“xxxxxxx”},
功能(状态){
var pushToken=状态;
console.warn('push-token:'+pushToken);
//启动我的ajax将令牌插入mysql
var param={Token:pushToken};
$.ajax({
网址:'http://luxurylebanon.com/offeratlive/apitoken.php,数据:param,数据类型:'json',成功:函数(结果)
{
if(result.success==false)
{
警报(失败)
}
否则{
警报(成功)
}
}
});
//结束ajax
},
功能(状态){
console.warn(JSON.stringify(['failed to register',status]);
});
document.addEventListener('push-notification',函数(事件){
var title=event.notification.title;
var userData=event.notification.userData;
if(typeof(userData)!=“未定义”){
console.warn('user data:'+JSON.stringify(userData));
}
navigator.notification.alert(标题);
});
}
PHP apitoken.PHP
看过你的代码后,我认为它有一些错误。
因此,让我们尝试修复它们:
首先。在PushNotification.js之前是否包含jquery js脚本?否则,将不执行“$.ajax”
另一件事。ajax的默认类型是GET,在php代码中使用POST。
而且你根本不用json。所以你的代码应该转换成这样
$.ajax({
type: "POST",
async: true,
url: url,
data: params,
success: function (result) {
// todo
},
error: function (result) {
// todo
}
});
最后一件事。param var应按如下方式初始化:
var param=“Token=”+pushToken
希望这会有帮助。在查看了您的代码后,我认为其中包含一些错误。
因此,让我们尝试修复它们:
首先。在PushNotification.js之前是否包含jquery js脚本?否则,将不执行“$.ajax”
另一件事。ajax的默认类型是GET,在php代码中使用POST。
而且你根本不用json。所以你的代码应该转换成这样
$.ajax({
type: "POST",
async: true,
url: url,
data: params,
success: function (result) {
// todo
},
error: function (result) {
// todo
}
});
最后一件事。param var应按如下方式初始化:
var param=“Token=”+pushToken
希望这会有帮助。我也遇到了同样的问题,我更新了Pushwoosh.jar,它对我很有用。:) 我也遇到了同样的问题,我更新了Pushwoosh.jar,它对我有效。:) 谢谢。它确实有帮助,但现在我有一个奇怪的问题,我上面列出了。谢谢。它确实有帮助,但现在我有一个奇怪的问题,我上面列出了。