Android谷歌云消息传递

时间:2014-04-24 06:17:23

标签: android google-api-php-client

我尝试将邮件从我的服务器发送到谷歌云服务器,但我有问题... 我获得服务器密钥(服务器应用程序的密钥),将其设置为此代码:

 $headers = array(
    'Authorization: key=' .My server key,
    'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);

发送消息后我有这样的结果:

未经授权 错误401

我使用这样的ip:78.47.150.20

但是当我使用test ip 0.0.0.0/0时我没有任何问题......

1 个答案:

答案 0 :(得分:1)

function sendNotification($registrationIdsArray, $messageData) {
  $apiKey = "YOUR_API_KEY";

 $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" .   $apiKey);
 $data = array(
 'data' => $messageData,
 'registration_ids' => $registrationIdsArray
 );

$ch = curl_init();

  curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

$response = curl_exec($ch);
curl_close($ch);

return $response;
}