duanhun3273 2013-01-15 09:46
浏览 40
已采纳

Android中的错误:使用PHP的GCM

I used following code in PHP to send GCM message to Android:

<?php
$apiKey = "xxxxx"; //my api key


$registrationIDs = array("APA91bGGN7o7AwVNnv35lwP5Jw8OTJQL331XcxPfEIu4xt-ZKLe6R0aSSbAve99uKSDXhzE9L2PVLihpqFt0DEawhymUs9h5ICbTMweMAEJypg6ZLFqmf6SOGlyULQzudw9MM1DjbPaaKbo--wxWoHGkjyec2H_63e7mesYjaRf4_rgxBe655M0");



// Message to be sent
$message = "Message Text";

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

$headers = array( 
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                );

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

echo $result;

It's working fine. Here I added my device tokens manually(hardcoded).

But when i tried getting device tokens from the database, I am getting below mentioned error.

Code:

<?php
include 'config.php';
if($_REQUEST['msg']!='')
{
    echo $message = $_REQUEST['msg']; 
// Replace with real BROWSER API key from Google APIs
$apiKey ="xxxxxx"; //my api key    
// Replace with real client registration IDs 
$sql = mysql_query("SELECT `device_token` FROM `users`");
$result = mysql_fetch_array($sql);


$registrationIDs = $result;

// Message to be sent
$message = $_REQUEST['msg'];

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

$headers = array( 
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                );

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

echo $result;

}

I also tried using json_encode($registration_ids) with no use.

"registration_ids" field is not a JSON array
  • 写回答

2条回答 默认 最新

  • dongranding3909 2013-01-15 11:52
    关注

    I finally got a solution. I used a for loop to get all the results into an array and converted array into JSON array using json_encode();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?