GCM 消息未在 PHP 内到达目的地
GCM message does not reach destination in PHP
我正在尝试发送 GCM 通知给定服务器操作 sendGroupMessage
。目前,虽然我已经实现了它的功能以响应 echo $testGCM;
,但 GCM 消息没有发送
case "sendGroupMessage":
if ($userId = authenticateUser($db, $username, $password, $gcmregid))
{
if (isset($_REQUEST['toGroupId']))
{ // instead of toUserName it's to a groupId
$toGroupName = $_REQUEST['toGroupName'];
$toGroupId = $_REQUEST['toGroupId'];
$message = $_REQUEST['messageText'];
$campaign = $_REQUEST['campaign_id'];
$location = $_REQUEST['location_id'];
// Query to get the users id's who are in the group but not the user sending the message
$sqlGroupMembers = "SELECT DISTINCT usersId from users_groups
WHERE usersId != '".$userId."' AND groupId = '".$toGroupId."'";
// Loop to create a copy of message for all users taht are part of that group
if($getGroupMembersId = $db->query($sqlGroupMembers))
{
while($rowGroupMembers = $db -> fetchObject($getGroupMembersId))
{
$sql22 = "INSERT INTO `group_messages` (`myId`,`fromUser`, `fromUId`,`toGroupName`,`toGroupId`, `sentdt`, `read`, `readdt`, `messageText`,`shared_campaign_id`,`shared_campaign_location_id`)
VALUES ('".$rowGroupMembers->usersId."','".$username."', '".$userId."','".$toGroupName."', '".$toGroupId."', '".DATE("Y-m-d H:i")."', '0', '".DATE("Y-m-d H:i")."', '".$message."','".$campaign."','".$location."');";
error_log("$sql22", 3 , "error_log");
if ($db->query($sql22))
{
$out = SUCCESSFUL;
echo testGCM(); // Testing the actions
}
else
{
$out = FAILED;
}
}
}
}
else
{
$out = FAILED;
}
}
else
{
$out = FAILED;
}
break;
测试 GCM 操作:
function testGCM() {
$gcmRegID = '(users GCM regID)'
$pushMessage = $_POST["syncNewData"];
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("syncNewData" => $pushMessage);
$pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);
}
}
发送通知方法:
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", "(Google 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;
}
您的 testGCM() 函数似乎有问题。您可以记录 $pushStatus
变量以查看那里有什么或者它是否为空。也没有代码解析请求后返回的 JSON 数组。您可以查看 logcat 中的错误日志来调查问题。
这里有一些教程可以帮助您找到有关实施问题的更多信息:
https://github.com/mattg888/GCM-PHP-Server-Push-Message
http://www.androidbegin.com/tutorial/android-google-cloud-messaging-gcm-tutorial/
希望对您有所帮助!!!
我正在尝试发送 GCM 通知给定服务器操作 sendGroupMessage
。目前,虽然我已经实现了它的功能以响应 echo $testGCM;
case "sendGroupMessage":
if ($userId = authenticateUser($db, $username, $password, $gcmregid))
{
if (isset($_REQUEST['toGroupId']))
{ // instead of toUserName it's to a groupId
$toGroupName = $_REQUEST['toGroupName'];
$toGroupId = $_REQUEST['toGroupId'];
$message = $_REQUEST['messageText'];
$campaign = $_REQUEST['campaign_id'];
$location = $_REQUEST['location_id'];
// Query to get the users id's who are in the group but not the user sending the message
$sqlGroupMembers = "SELECT DISTINCT usersId from users_groups
WHERE usersId != '".$userId."' AND groupId = '".$toGroupId."'";
// Loop to create a copy of message for all users taht are part of that group
if($getGroupMembersId = $db->query($sqlGroupMembers))
{
while($rowGroupMembers = $db -> fetchObject($getGroupMembersId))
{
$sql22 = "INSERT INTO `group_messages` (`myId`,`fromUser`, `fromUId`,`toGroupName`,`toGroupId`, `sentdt`, `read`, `readdt`, `messageText`,`shared_campaign_id`,`shared_campaign_location_id`)
VALUES ('".$rowGroupMembers->usersId."','".$username."', '".$userId."','".$toGroupName."', '".$toGroupId."', '".DATE("Y-m-d H:i")."', '0', '".DATE("Y-m-d H:i")."', '".$message."','".$campaign."','".$location."');";
error_log("$sql22", 3 , "error_log");
if ($db->query($sql22))
{
$out = SUCCESSFUL;
echo testGCM(); // Testing the actions
}
else
{
$out = FAILED;
}
}
}
}
else
{
$out = FAILED;
}
}
else
{
$out = FAILED;
}
break;
测试 GCM 操作:
function testGCM() {
$gcmRegID = '(users GCM regID)'
$pushMessage = $_POST["syncNewData"];
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("syncNewData" => $pushMessage);
$pushStatus = sendPushNotificationToGCM($gcmRegIds, $message);
}
}
发送通知方法:
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", "(Google 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;
}
您的 testGCM() 函数似乎有问题。您可以记录 $pushStatus
变量以查看那里有什么或者它是否为空。也没有代码解析请求后返回的 JSON 数组。您可以查看 logcat 中的错误日志来调查问题。
这里有一些教程可以帮助您找到有关实施问题的更多信息:
https://github.com/mattg888/GCM-PHP-Server-Push-Message
http://www.androidbegin.com/tutorial/android-google-cloud-messaging-gcm-tutorial/
希望对您有所帮助!!!