【问题标题】:error in Android: GCM using PHPAndroid 中的错误:使用 PHP 的 GCM
【发布时间】:2013-01-15 09:46:42
【问题描述】:

我在 PHP 中使用以下代码向 Android 发送 GCM 消息:

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

一切正常。在这里,我手动添加了我的设备令牌(硬编码)。

但是当我尝试从数据库中获取设备令牌时,我遇到了下面提到的错误。

代码:

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

}

我也尝试过使用json_encode($registration_ids),但没有用。

"registration_ids" field is not a JSON array

【问题讨论】:

    标签: php android google-cloud-messaging


    【解决方案1】:

    我终于找到了解决办法。我使用 for 循环将所有结果放入数组中,并使用 json_encode(); 将数组转换为 JSON 数组

    【讨论】:

      【解决方案2】:

      我遇到了同样的错误,因为我的数组 indexes 没有正确排序我使用 PHP 的 sort() 函数修复它。

      排序前

      $registration_ids = Array
      (
          [1] => "xxx"
          [6] => "xxx"
          [8] => "xxx"
      )
      

      排序

      sort($registration_ids);
      

      排序后

      $registration_ids = Array
      (
          [0] => "xxx"
          [1] => "xxx"
          [2] => "xxx"
      )
      

      传递给 PHP 的 cURL

      $fields = array(
          'registration_ids' => $ids,
          'data' => array('message' => $data)
      );
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-20
        • 1970-01-01
        相关资源
        最近更新 更多