php代码向所有设备发送通知GCM

php代码向所有设备发送通知GCM,php,Php,我需要一点帮助,一次向所有设备发送通知。 我知道需要使用数组来实现这一点,但我无法理解。一点提示也会奏效 DB table design id gcm_regid name email created_at 目前,我必须填写所有设备的表格,并逐一发送通知 PHP代码: <html> <head> <title></title> <meta http-equiv="Content-Type" cont

我需要一点帮助,一次向所有设备发送通知。 我知道需要使用数组来实现这一点,但我无法理解。一点提示也会奏效

DB table design
id    gcm_regid   name  email   created_at 
目前,我必须填写所有设备的表格,并逐一发送通知

PHP代码:

 <html>
   <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">    </script>
    <script type="text/javascript">
        $(document).ready(function(){

        });
        function sendPushNotification(id){
            var data = $('form#'+id).serialize();
            $('form#'+id).unbind('submit');
            $.ajax({
                url: "send_message.php",
                type: 'GET',
                data: data,
                beforeSend: function() {

                },
                success: function(data, textStatus, xhr) {
                      $('.txt_message').val("");
                },
                error: function(xhr, textStatus, errorThrown) {

                }
            });
            return false;
        }
    </script>
</head>
<body>
    <?php
    include_once 'db_functions.php';
    $db = new DB_Functions();
    $users = $db->getAllUsers();
    if ($users != false)
        $no_of_users = mysql_num_rows($users);
    else
        $no_of_users = 0;
    ?>
    <div class="container">
        <h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
        <hr/>
        <ul class="devices">
            <?php
            if ($no_of_users > 0) {
                ?>
                <?php
                while ($row = mysql_fetch_array($users)) {
                    ?>
                    <li>
                        <form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $row["id"] ?>')">
                            <label>Name: </label> <span><?php echo $row["name"] ?></span>
                            <div class="clear"></div>
                            <label>Email:</label> <span><?php echo $row["email"] ?></span>
                            <div class="clear"></div>
                            <div class="send_container">
                                <textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
                                <input type="hidden" name="regId" value="<?php echo $row["gcm_regid"] ?>"/>
                                <input type="submit" class="send_btn" value="Send" onclick=""/>
                            </div>
                        </form>
                    </li>
                <?php }
            } else { ?>
                <li>
                    No Users Registered Yet!
                </li>
            <?php } ?>
        </ul>
    </div>
</body>

$(文档).ready(函数(){
});
函数sendPushNotification(id){
var data=$('form#'+id).serialize();
$('form#'+id).unbind('submit');
$.ajax({
url:“send_message.php”,
键入:“GET”,
数据:数据,
beforeSend:function(){
},
成功:函数(数据、文本状态、xhr){
$('.txt_message').val(“”);
},
错误:函数(xhr、textStatus、errorshown){
}
});
返回false;
}
已注册的设备数量:


  • 你能把你的问题说清楚一点吗?您的send_message.php脚本在哪里?实际上,我想向所有设备发送公共消息。目前,上面的代码为每个设备提供了单独的表单。我只需要一个普通的表单,它应该将消息发送到所有设备,而不是复制粘贴消息然后发送。
      <?php
     if (isset($_GET["regId"]) && isset($_GET["message"])) {
    $regId = $_GET["regId"];
    $message = $_GET["message"];
    
    include_once './GCM.php';
    
    $gcm = new GCM();
    
    $registatoin_ids = array($regId);
    $message = array("price" => $message);
    
    $result = $gcm->send_notification($registatoin_ids, $message);
    
    echo $result;
    }
    ?>