Question

i try to send message from my server to google cloud server, but i have problem with it... i get server key(Key for server applications), set it to this code:

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

after sending message i have such result:

Unauthorized Error 401

I use such ip:78.47.150.20

but when i use test ip 0.0.0.0/0 i haven't any problems....

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top