我在跟着
http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/?通过GCM发送推送通知.一切正常,但是我可以将推送通知发送到一个设备.注册另一个设备将替换以前设备的注册ID.我在
http://javapapers.com/android/android-multicast-notification-using-google-cloud-messaging-gcm/试过了Shardool提供的解决方案?但它不工作.
任何建议都会有很大的帮助.
以下是我的gcm.PHP代码,注册设备并发送推送通知,但仅限于最近注册的单个设备.
gcm.PHP
<?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_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,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>
在这里,我用MysqL重写了PHP,而不是从文件中检索密钥.在这种情况下,我从表中检索所有的regIds并将它们放在一个数组中,并将其传递给sendPushNotification函数以将消息推送到所有.这里有2个文件,一个连接到数据库,另一个用于GCM:
原文链接:https://www.f2er.com/php/139839.htmlconnect.PHP:
<?PHP $db_host = "localhost"; $db_user = "root"; //change to your database username,it is root by default $db_pass = ''; //change to your database password,for XAMPP it is nothing for WAMPP it is root $db_db = 'gcmFirst'; //change to your database name if(!@MysqL_connect($db_host,$db_user,$db_pass) || !@MysqL_select_db($db_db)) { die('couldnt connect to database ' .MysqL_error()); } ?>
gcm.PHP
<?PHP require 'connect.PHP'; function sendPushNotification($registration_ids,$message) { $url = 'https://android.googleapis.com/gcm/send'; $fields = array( 'registration_ids' => $registration_ids,); define('GOOGLE_API_KEY','AIzaSyCjctNK2valabAWL7rWUTcoRA-UAXI_3ro'); $headers = array( 'Authorization:key=' . GOOGLE_API_KEY,'Content-Type: application/json' ); echo json_encode($fields); $ch = curl_init(); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,json_encode($fields)); $result = curl_exec($ch); if($result === false) die('Curl Failed ' . curl_error()); curl_close($ch); return $result; } $pushStatus = ''; if(!empty($_GET['push'])) { $query = "SELECT regId FROM users"; if($query_run = MysqL_query($query)) { $gcmRegIds = array(); while($query_row = MysqL_fetch_assoc($query_run)) { array_push($gcmRegIds,$query_row['regId']); } } $pushMessage = $_POST['message']; if(isset($gcmRegIds) && isset($pushMessage)) { $message = array('message' => $pushMessage); $pushStatus = sendPushNotification($gcmRegIds,$message); } } if(!empty($_GET['shareRegId'])) { $gcmRegId = $_POST['regId']; $query = "INSERT INTO users VALUES ('','$gcmRegId')"; if($query_run = MysqL_query($query)) { 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 = 'Messages to Transmit via GCM'></textarea> </div> <div> <input type = 'submit' value = 'Send Push Notification via GCM'> </div> <p><h3><?PHP echo $pushStatus ?></h3></p> </form> </body> </html>
所有你需要做的是创建一个数据库,其中具有id,regId和name作为列的users表.
希望这是你正在寻找的