PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – 使用GCM向多个Android设备发送推送通知大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在跟着 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>

请告诉我如何在GCMRegid.txt中存储多个设备注册ID,并将通知发送到每个注册的设备

在这里,我用MysqL重写了PHP,而不是从文件中检索密钥.在这种情况下,我从表中检索所有的regIds并将它们放在一个数组中,并将其传递给sendPushNotification函数以将消息推送到所有.这里有2个文件,一个连接到数据库,另一个用于GCM:

connect.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表.

希望这是你正在寻找的

大佬总结

以上是大佬教程为你收集整理的php – 使用GCM向多个Android设备发送推送通知全部内容,希望文章能够帮你解决php – 使用GCM向多个Android设备发送推送通知所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。