我正在尝试在sendGroupMessage
的服务器操作下发送GCM通知。目前,GCM消息没有发送,虽然我已经实现了其“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;
testGCM行动:
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);
}
}
sendNotification方法:
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;
}
答案 0 :(得分:1)
您的testGCM()函数似乎存在一些问题。您是否可以记录$pushStatus
变量以查看其中的内容或是否为null。此外,没有代码可以解析请求后返回的JSON数组。您可以在logcat中检查错误日志以调查问题。
以下是一些可以帮助您找到有关实施问题的更多信息的教程:
https://github.com/mattg888/GCM-PHP-Server-Push-Message
http://www.androidbegin.com/tutorial/android-google-cloud-messaging-gcm-tutorial/
希望这有帮助!!!