【问题标题】:Google Cloud Messaging- Sending Push Message To Android Using PHPGoogle Cloud Messaging - 使用 PHP 向 Android 发送推送消息
【发布时间】:2014-02-21 17:58:00
【问题描述】:

我正在开发一个需要使用 GCM 推送消息的 android 项目。 我想只使用 PHP 而不是 HTML 发送通知。我如何实现这一目标?我尝试的方法只是将 gcm_regid 和 message 作为参数放入 $gcm->send_notification($registration_ids,$msg) 但我一直收到错误消息:- 字段“data”必须是 JSON 数组:["You Have A Notification!"]

在哪里“你有一个通知!”是我的味精!!请帮忙!!谢谢!

send_notification 的代码是

 public function send_notification($registatoin_ids, $message) {

    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;
}

}

这是使用按钮的点击发送消息的 index.php。但是,我需要在后台自动执行此操作而无需按下任何按钮,并希望在 PHP 中实现它。

<!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>

【问题讨论】:

  • 你使用下面给出的答案成功发送通知我也面临同样的问题
  • @user3233280 是的!这是很久以前的事了,所以不太记得位置,但我认为 GCM PHP 在发送消息函数中有一个名为 price 的参数,需要将其更改为我在 android 接收函数中设置的变量名。希望我清楚!

标签: php android html json google-cloud-messaging


【解决方案1】:

也许这会起作用:

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

【讨论】:

  • 似乎是解决方案,但它不发送消息。它发送一个空白消息而不是指定的消息。我会努力弄清楚的!谢谢:)
【解决方案2】:

您还需要在数组中嵌入registration_id。

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

【讨论】:

    【解决方案3】:
    $fields[] = array(
                'registration_ids' =>$registatoin_ids,
                'data' =>$message
            );
    

    使用带有 $fields 的方括号,这将使它成为一个 json 数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 2016-09-22
      相关资源
      最近更新 更多