GCM 发件人 ID 不匹配
GCM Sender Id Mismatch
我已经使用 API 密钥设置了第三方 GCM 服务器,使用 APP_SERVER_URL= "http://(localIPAddress):4430/gcm.php?shareRegId=1"
设置了 Android 应用程序并放置了我的计算机 public IP 地址,不是Google 应用程序控制台中 "Allowed IPs" 中的 localIPAdderss。
Android 应用指出它的 Registration_id 与第三方服务器共享,但在测试从我的服务器向应用发送消息时它指出:
{"multicast_id":7181230864355412704,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
我怎样才能让它工作?
gcm.php 在端口 4430 上的 Apache htdocs 中:
<?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", "(my console API key)"); // My API Key form Google console
$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;
}
?>
<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>
<p><h3><?php echo $pushStatus; ?></h3></p>
</body>
</html>
教程来源:http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/
您调用 gcm.register(senderID)
时使用的发件人 ID 似乎不是您服务器的发件人 ID。您必须在 Android 代码中仔细检查这一点。它是您的项目在 Google Developers Console 中的项目编号。
错误的完整描述是 here。
我已经使用 API 密钥设置了第三方 GCM 服务器,使用 APP_SERVER_URL= "http://(localIPAddress):4430/gcm.php?shareRegId=1"
设置了 Android 应用程序并放置了我的计算机 public IP 地址,不是Google 应用程序控制台中 "Allowed IPs" 中的 localIPAdderss。
Android 应用指出它的 Registration_id 与第三方服务器共享,但在测试从我的服务器向应用发送消息时它指出:
{"multicast_id":7181230864355412704,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
我怎样才能让它工作?
gcm.php 在端口 4430 上的 Apache htdocs 中:
<?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", "(my console API key)"); // My API Key form Google console
$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;
}
?>
<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>
<p><h3><?php echo $pushStatus; ?></h3></p>
</body>
</html>
教程来源:http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/
您调用 gcm.register(senderID)
时使用的发件人 ID 似乎不是您服务器的发件人 ID。您必须在 Android 代码中仔细检查这一点。它是您的项目在 Google Developers Console 中的项目编号。
错误的完整描述是 here。