Android 谷歌云消息传递[英] Android google cloud messaging

问题描述

我尝试将消息从我的服务器发送到谷歌云服务器,但我遇到了问题...我得到服务器密钥(服务器应用程序的密钥),将其设置为以下代码:

 $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 我没有任何问题....

推荐答案

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;
}
查看更多