1 pièce(s) jointe(s)
[Serveur GCM] erreur 401 Unauthorized
Bonsoir, bonjour,
Je recherche une solution à mon problème, j'ai passé des heures à chercher sur le net, mais en vain. J'espère que vous arriverez à m'aider à sortir de cette impasse :(.
Je suis en train de mettre en place un serveur GCM en php, en local.
J'ai suivi le tuto suivant http://javapapers.com/android/google...notifications/.
J'arrive bien à récupérer le regid que j'ai mis dans le fichier GCMRegId.txt pour faire le test au niveau du serveur.
Malheureusement, le serveur GCM n'arrive pas à communiquer avec le serveur Google. Dans le projet Google que j'ai créé, j'ai autorisée les ip suivants: 0.0.0.0/0, 127.0.0.1, ip privée, ip publique à faire des requêtes sur le serveur Google mais rien n'y fait, il y a toujours cette erreur qui persiste.
Console Google developer:
Pièce jointe 150889
Serveur GCM en php:
gcm.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
<?php
//generic php function to send GCM push notification
function sendPushNotificationToGCM($registatoin_ids, $message) {
//Google cloud messaging GCM-API url
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "********************");
$headers = array(
'Authorization: key=' . GOOGLE_API_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);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
<?php
//this block is to post message to GCM on-click
$pushStatus = "";
if(!empty($_GET["push"])) {
$gcmRegID = file_get_contents("GCMRegId.txt");
$pushMessage = $_POST["message"];
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);
}
}
//this block is to receive the GCM regId from external (mobile apps)
if(!empty($_GET["shareRegId"])) {
$gcmRegID = $_POST["regId"];
file_put_contents("GCMRegId.txt",$gcmRegID);
echo "Ok!";
exit;
}
?>
<p><h3><?php echo $pushStatus; ?></h3></p>
<p><h3><?php echo $gcmRegID; ?></h3></p>
<p><h3><?php echo $pushMessage; ?></h3></p> |
index.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<html>
<head>
<title>Google Cloud Messaging (GCM) Server in PHP</title>
</head>
<body>
<h1>Google Cloud Messaging (GCM) Server in PHP</h1>
<form method="post" action="gcm.php/?push=1">
<div>
<textarea rows="2" name="message" cols="23" placeholder="Message to transmit via GCM"></textarea>
</div>
<div><input type="submit" value="Send Push Notification via GCM" /></div>
</form>
</body>
</html> |
Merci de m'avoir lu!:D