I try to create project with use GoogleCloudMessaging to recieve push - notifications from my remote server. For client i use google tutorial Implementing GCM Client. For server i use this realization on php, but after i started php script, i got an error:
Unauthorized
Error 401
Here is my php code:
$url = 'https://android.googleapis.com/gcm/send';
$serverApiKey = "778688687533"; // sender id (Project Number)
$reg = "APA91bFFWLYsX8TK_XiGQJeUkHb_7FJZwZBSdnNEdxzQEQ-ijpmVvwBPxaz3wQPmWy_K6YAIYt1x_3IBDzbyALxcpmjL8mKEX7g5CV9QpXod3oFp4Hm5HqsY8snoQdua3l0NcMoHm0WE4oTbNUYcbAOE7zjJEV_p9Q"; // registration id
$headers = array(
'Content-Type:application/json',
'Authorization:key=' . $serverApiKey
);
$data = array(
'registration_ids' => array($reg),
'data' => array(
'message' => 'Hello, World!'
));
print (json_encode($data) . "\n\n");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
curl_close($ch);
print ($response);
To create the new server key, follow the steps,
This key is to be used for sending message in your script on server side.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.