Android推送通知GCM

Rav*_*mar 0 android push-notification google-cloud-messaging

我正在为我的应用程序使用android GCM向用户发送推送通知.我正在学习本教程

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

但在本教程中,它显示我们可以将推送通知发送到单个设备.但我想立即向所有用户发送推送通知.那么我该如何实现这项技术呢.

小智 5

我已经遇到了同样的问题并解决了它.要立即向所有用户发送推送通知,您必须执行您提到的教程中列出的所有内容.但你必须改变两个文件:index.phpsend_message.php

  1. index.php:

    一个.用以下内容替换JavaScript代码:

    $(document).ready(function(){
    
    });
    function sendToAll(){
        var data = $("#sendtoall").serialize();
        $("#sendtoall").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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    湾 用以下内容替换body标签内的代码(跳过前五行):

    <div class="container">
        <h1>No of Devices Registered: <?php echo $no_of_users; ?></h1>
        <hr/>
        <ul class="devices">
            <?php
            if ($no_of_users > 0) {
                ?>
                <li>
                    <form id="sendtoall" name="" method="post" onsubmit="return sendToAll()">
                        <div class="send_container">                               
                            <textarea rows="3" name="message" cols="25" class="txt_message" placeholder="Type message here"></textarea>
                            <?php
                            while ($row = mysql_fetch_array($users)) {
                            ?>
                            <input type="hidden" name="regId[]" value="<?php echo $row["gcm_regid"]; ?>"/>
                            <?php
                            }
                            ?>
                            <input type="submit" class="send_btn" value="Send To All" onclick=""/>
                        </div>
                    </form>
                </li>
                <?php
            } else { ?>
                <li>
                    No Users Registered Yet!
                </li>
            <?php } ?>
        </ul>
    </div>
    <div class="clear"></div>
    
    Run Code Online (Sandbox Code Playgroud)
  2. send_message.php:

    替换线:

    $registatoin_ids = array($regId);
    
    Run Code Online (Sandbox Code Playgroud)

    $registatoin_ids = $regId; 
    
    Run Code Online (Sandbox Code Playgroud)