问题描述
如何在设备注册检索设备ID /令牌?我使用的PhoneGap Pushwoosh例子,它工作正常。但我无法弄清楚如何检索在设备注册initPushwoosh令牌。
How to retrieve device id/ token at device registration? I am using Phonegap Pushwoosh example and it works fine. But I could not figure out how to retrieve the token at device registration initPushwoosh.
我不是一个专业的程序员。任何帮助将AP preciated。
I am not a professional programmer. Any help will be appreciated.
我有初始化一个index.html<身体的onload =的init();>
I have an index.html that initialize<body onload="init();">
在main.js
function init() {
document.addEventListener("deviceready", deviceInfo, true);
document.addEventListener("deviceready", initPushwoosh, true);
}
在PushNotification.js
In 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
The first section is the .registerDevice and the token is probably pushToken, but I just cannot figure out how to retrieve it from this function!The best is to send it to a MySQL database lets call it smartphonedb.tokentable
我修改了initPushwoosh()使用Ajax(见下文),我在MySQL没有收到令牌送我到MySQL。我是否发出正确的令牌参数(pushToken)?
I modified the initPushwoosh() to send me the token to MySQL using Ajax (see below) I am receiving nothing on MySQL. Am I sending the right Token param (pushToken)?
function initPushwoosh()
{
var pushNotification = window.plugins.pushNotification;
// CHANGE projectid & appid
pushNotification.registerDevice({ projectid: "xxxxxx", appid : "xxxxxxx" },
function(status) {
var pushToken = status;
console.warn('push token: ' + pushToken);
// start my ajax to insert token to mysql
var param ={Token: pushToken};
$.ajax({
url: 'http://luxurylebanon.com/offeratlive/apitoken.php', data: param, dataType: 'json', success: function(result)
{
if(result.success == false)
{
alert(failed)
}
else {
alert(success)
}
}
});
// end ajax
},
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);
});
}
The PHP apitoken.php
<?php
$username="xxxxxxx";
$password="xxxxxxxxxxxx";
$database="offeratdb";
$server="offeratdb.db.xxxxxxxxx.com";
$connect = mysql_connect($server,$username,$password)or die('Could not connect: ' . mysql_error());
@mysql_select_db($database) or die('Could not select database ('.$database.') because of : '.mysql_error());
$vtoken= $_POST['Token'];
// Performing SQL query
$query = "INSERT INTO `tokentable` (`thetoken`) VALUES ('$vtoken')";
$result = mysql_query($query)or die('Query failed: ' . mysql_error());
echo $vtoken;
// We will free the resultset...
mysql_free_result($result);
// Now we close the connection...
mysql_close($connect);
?>
任何帮助将AP preciated
any help will be appreciated
推荐答案
通过您的code细算我认为它包含了一些错误。
所以,让我们试着解决这些问题:
After looking through your code I think it contains some mistakes.So, lets try to fix them:
-
首先。你有PushNotification.js之前包括jQuery的js脚本?如果不是,$就将不被执行。
First of all. Do you have jquery js script included before PushNotification.js? If not, "$.ajax" will not be executed.
其他的事情。 AJAX的默认类型是GET,你在你的PHP code使用POST。
而你不使用JSON的。所以,你的code应该转变成类似这样
The other thing. The ajax default type is GET, and you use POST in your php code.And you don't use json at all. So your code should be transformed into something like this
$.ajax({
type: "POST",
async: true,
url: url,
data: params,
success: function (result) {
// todo
},
error: function (result) {
// todo
}
});
和的最后一件事。帕拉姆VAR应该初始化是这样的:
VAR参数=令牌=+ pushToken;
And the last thing. The param var should be initialized like this:var param = "Token="+pushToken;
希望这将是有益的。
这篇关于PhoneGap的/ Android的Pushwoosh检索设备ID /令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!