在多台设备发送推送通知 [英] Sending Push Notification on multiple devices

查看:115
本文介绍了在多台设备发送推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按照这个教程,使我的Andr​​oid应用程序的推送通知

<一个href="http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/" rel="nofollow">http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

我已经得到了这个教程的工作,但我的问题是。它只能由一个发送通知之一。我需要通过只发送一次通知发送到所有gcm_id或注册的ID。

难道人已经解决了这个问题?

这是我的index.php

 &LT;! - 
要改变这种模板,选择工具|模板
和打开,在编辑的模板。
 - &GT;
&LT;!DOCTYPE HTML&GT;
&LT; HTML&GT;
    &LT; HEAD&GT;
        &LT;冠军&GT;&LT; /标题&GT;
        &LT; META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8&GT;
        &所述;脚本的src =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&GT;&所述; /脚本&GT;
        &LT;脚本类型=文/ JavaScript的&GT;
            $(文件)。就绪(函数(){

            });
            功能sendPushNotification(ID){
                VAR数据= $('#形式'+ id)的.serialize();
                $('#形式'+ id)的.unbind('提交');
                $阿贾克斯({
                    网址:send_message.php
                    键入:GET,
                    数据:数据,
                    beforeSend:函数(){

                    },
                    成功:功能(数据,textStatus,XHR){
                          $('。txt_message')VAL()。
                    },
                    错误:函数(XHR,textStatus,errorThrown){

                    }
                });
                返回false;
            }
        &LT; / SCRIPT&GT;
        &LT;风格类型=文本/ CSS&GT;
            。容器{
                宽度:950px;
                保证金:0汽车;
                填充:0;
            }
            H1 {
                字体家庭:Helvetica Neue字体,黑体,宋体,无衬线;
                字体大小:24PX;
                颜色:#777;
            }
            div.clear {
                明确:两个;
            }
            ul.devices {
                保证金:0;
                填充:0;
            }
            ul.devices李{
                浮动:左;
                列表样式:无;
                边界:1px的固体#dedede;
                填充:10px的;
                保证金:0 15px的25像素0;
                边界半径:的3px;
                -webkit-箱阴影:0 1px的5像素RGBA(0,0,0,0.35);
                -moz-箱阴影:0 1px的5像素RGBA(0,0,0,0.35);
                箱阴影:0 1px的5像素RGBA(0,0,0,0.35);
                字体家庭:Helvetica Neue字体,黑体,宋体,无衬线;
                颜色:#555;
            }
            ul.devices李标签,ul.devices李跨度{
                字体家庭:Helvetica Neue字体,黑体,宋体,无衬线;
                字体大小:12px的;
                字体风格:正常;
                字体变形:正常;
                字体重量:大胆的;
                颜色:#393939;
                显示:块;
                浮动:左;
            }
            ul.devices李标签{
                高度:25像素;
                宽度:50像素;
            }
            ul.devices里的textarea {
                浮动:左;
                调整:无;
            }
            ul.devices李.send_btn {
                背景:-webkit梯度(直链的,0%0%0%100%,从(#0096FF)至(#005DFF));
                背景:-webkit线性梯度(0%0%0%100%,从(#0096FF)至(#005DFF));
                背景:-moz-线性渐变(中心顶部,#0096FF,#005DFF);
                背景:线性渐变(#0096FF,#005DFF);
                文字阴影:0 0 1px的RGBA(0,0,0,0.3);
                边界半径:的3px;
                颜色:#FFF;
            }
        &LT; /风格&GT;
    &LT; /头&GT;
    &LT;身体GT;
        &LT; PHP
        include_once'db_functions.php;
        $ DB =新DB_Functions();
        $用户= $ DB-&GT; getAllUsers();
        如果($用户!= FALSE)
            $ no_of_users = mysql_num_rows($用户);
        其他
            $ no_of_users = 0;
        ?&GT;
        &LT; D​​IV CLASS =容器&GT;
            &LT; H1&GT;没有设备注册的:&LT; PHP的echo $ no_of_users; ?&GT;&LT; / H1&GT;
            &LT;小时/&GT;
            &LT; UL类=设备&GT;
                &LT; PHP
                如果($ no_of_users大于0){
                    ?&GT;
                    &LT; PHP
                    而($行= mysql_fetch_array($用户)){
                        ?&GT;
                        &LT;李&GT;
                            &LT;形式ID =&LT;?PHP的回声$行[身份证]&GT;? NAME =的方法=邮报的onsubmit =返回sendPushNotification('&LT;?PHP的回声$行[身份证]&GT;')&GT;
                                &LT;标签&gt;名称:LT; /标签&gt; &LT;跨度&GT;&LT; PHP的echo $行[名称]&GT;&LT; / SPAN&GT;
                                &LT; D​​IV CLASS =清除&GT;&LT; / DIV&GT;
                                &LT;标签&gt;电子邮件:LT; /标签&gt; &LT;跨度&GT;&LT; PHP的echo $行[电子邮件]&GT;&LT; / SPAN&GT;
                                &LT; D​​IV CLASS =清除&GT;&LT; / DIV&GT;
                                &LT; D​​IV CLASS =send_container&GT;
                                    &LT; textarea的行数=3NAME =消息COLS =25级=txt_message占位符=此类型的消息&GT;&LT; / textarea的&GT;
                                    &LT;输入类型=隐藏名称=REGID值=&LT;?PHP的回声$行[gcm_regid]&GT;/&GT;
                                    &LT;输入类型=提交级=send_btn值=发送的onclick =/&GT;
                                &LT; / DIV&GT;
                            &LT; /形式GT;
                        &LT; /李&GT;
                    &LT; PHP}
                ?}其他{&GT;
                    &LT;李&GT;
                        没有用户注册吗!
                    &LT; /李&GT;
                &LT; PHP}&GT?;
            &LT; / UL&GT;
        &LT; / DIV&GT;
    &LT; /身体GT;
&LT; / HTML&GT;
 

send_message.php

 &LT; PHP

/ *
 *要改变这种模板,选择工具|模板
 *和打开在编辑器中的模板。
 * /
如果(使用isset($ _ GET [REGID])及和放大器;使用isset($ _ GET [信息])){
    $ REGID = $ _GET [REGID];
    $消息= $ _GET [信息];

    include_once'./GCM.php;

    $ GCM =新GCM();

    $ registatoin_ids =阵列($ REGID);
    $消息=阵列(价格=&GT; $消息);

    $结果= $ gcm-&GT; send_notification($ registatoin_ids,$消息);

    回声$结果;
}
?&GT;
 

GCM.php

 &LT; PHP

/ *
 *要改变这种模板,选择工具|模板
 *和打开在编辑器中的模板。
 * /

/ **
 * GCM说明
 *
 * @author拉维玉田
 * /
一流的GCM {

    //把你的code在这里
    //构造
    功能__construct(){

    }

    / **
     *发送推送通知
     * /
    公共职能send_notification($ registatoin_ids,$消息){
        //包括配置
        include_once'./config.php;

        //设置POST变量
        $ URL ='https://android.googleapis.com/gcm/send';

        $领域=阵列(
            registration_ids'=&GT; $ registatoin_ids,
            '数据'=&GT; $消息,
        );

        $标题=阵列(
            授权:键='。 GOOGLE_API_KEY,
            内容类型:应用程序/ JSON
        );
        //打开连接
        $ CH = curl_init();

        //设置URL,POST数瓦尔,POST数据
        curl_setopt($ CH,CURLOPT_URL,$网址);

        curl_setopt($ CH,CURLOPT_POST,真正的);
        curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
        curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);

        //禁用SSL证书支持,即暂时
        curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,假);

        curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));

        //执行后
        $结果= curl_exec($ CH);
        如果($结果=== FALSE){
            死亡('卷曲失败:curl_error($ CH)。);
        }

        //关闭连接
        curl_close($ CH);
        回声$结果;
    }

}

?&GT;
 

解决方案

我没有EXP在PHP,但我可以告诉你,你可以修改code的地方。

请一组ID并投入 registration_ids

在最后,你必须拥有的所有ID为 registatoin_ids

  $领域=阵列(
            registration_ids'=&GT; $ registatoin_ids,//这
            '数据'=&GT; $消息,
        );
 

send_notification($ registatoin_ids,$消息)

在当前有它,所以它是发送通知到单一设备单reg_id。

I'm trying to make an push notification on my android app by following this tutorial

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

I already got this tutorial working, but my problem is. It only send notification one by one. I need to send the notification to all gcm_id or registered id by just sending once.

Do anyone already solved this issue?

here is my index.php

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<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>
        <style type="text/css">
            .container{
                width: 950px;
                margin: 0 auto;
                padding: 0;
            }
            h1{
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                font-size: 24px;
                color: #777;
            }
            div.clear{
                clear: both;
            }
            ul.devices{
                margin: 0;
                padding: 0;
            }
            ul.devices li{
                float: left;
                list-style: none;
                border: 1px solid #dedede;
                padding: 10px;
                margin: 0 15px 25px 0;
                border-radius: 3px;
                -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                -moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                color: #555;
            }
            ul.devices li label, ul.devices li span{
                font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
                font-size: 12px;
                font-style: normal;
                font-variant: normal;
                font-weight: bold;
                color: #393939;
                display: block;
                float: left;
            }
            ul.devices li label{
                height: 25px;
                width: 50px;                
            }
            ul.devices li textarea{
                float: left;
                resize: none;
            }
            ul.devices li .send_btn{
                background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
                background: -webkit-linear-gradient(0% 0%, 0% 100%, from(#0096FF), to(#005DFF));
                background: -moz-linear-gradient(center top, #0096FF, #005DFF);
                background: linear-gradient(#0096FF, #005DFF);
                text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
                border-radius: 3px;
                color: #fff;
            }
        </style>
    </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>
</html>

send_message.php

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
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;
}
?>

GCM.php

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GCM
 *
 * @author Ravi Tamada
 */
class GCM {

    //put your code here
    // constructor
    function __construct() {

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once './config.php';

        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
        echo $result;
    }

}

?>

解决方案

I am not having exp in PHP but I can tell you the places where you can modify your code.

Make an array of ids and put into registration_ids.

At the end you must have all ids into registatoin_ids at

$fields = array(
            'registration_ids' => $registatoin_ids, // HERE IT IS
            'data' => $message,
        );

into send_notification($registatoin_ids, $message).

In current it having single reg_id so it is sending notification to single device.

这篇关于在多台设备发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆