Phonegap/Pushwoosh Android retrieving Device id /

2019-07-22 19:13发布

问题:

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.

I am not a professional programmer. Any help will be appreciated.

I have an index.html that initialize <body onload="init();">

In main.js

function init() { 
  document.addEventListener("deviceready", deviceInfo, true);
  document.addEventListener("deviceready", initPushwoosh, true);
}

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);
      });

 }

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

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);
?>

any help will be appreciated

回答1:

After looking through your code I think it contains some mistakes. So, lets try to fix them:

  1. First of all. Do you have jquery js script included before PushNotification.js? If not, "$.ajax" will not be executed.

  2. 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
        }
    });
    
  3. And the last thing. The param var should be initialized like this: var param = "Token="+pushToken;

Hope this would be helpful.



回答2:

I was having the same problem, I updated the Pushwoosh.jar and it worked for me. :)