Php GCM不在设备上显示内容

Php GCM不在设备上显示内容,php,android,push-notification,Php,Android,Push Notification,我尝试用PHP将通知推送到android设备。当我在文本字段中插入文本并将其发送到android设备时。它发送成功,但设备上的消息中没有内容。为什么?我该如何修复它? 这是我的PHP代码: <?php include('config/dbconnect_log.php'); if(isset($_POST['submit'])){ function sendPushNotification($registration_ids, $message) { $url = 'https:

我尝试用PHP将通知推送到android设备。当我在文本字段中插入文本并将其发送到android设备时。它发送成功,但设备上的消息中没有内容。为什么?我该如何修复它? 这是我的PHP代码:

<?php
include('config/dbconnect_log.php');
if(isset($_POST['submit'])){
function sendPushNotification($registration_ids, $message) {

    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registration_ids,
        'data' => $message,
    );

    define('GOOGLE_API_KEY', 'my google api key');

    $headers = array(
        'Authorization:key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    echo json_encode($fields);
    $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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if($result === false)
        die('Curl failed ' . curl_error());

    curl_close($ch);
    return $result;

}

$pushStatus = '';

    $query = "SELECT DISTINCT googleid FROM ggevent";
    if($query_run = mysql_query($query)) {

        $gcmRegIds = array();
        $i = 0;
        while($query_row = mysql_fetch_assoc($query_run)) {
            $i++;
            $gcmRegIds[floor($i/1000)][] = $query_row['googleid'];
        }

    }
    $pushMessage = $_POST['message'];
    if(isset($gcmRegIds) && isset($pushMessage)) {

        $message = array('msg' => $pushMessage);
        $pushStatus = array();
        foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotification($val, $message);

    }   

}
?>

<html>
    <head>

    </head>
    <body>
  <article class="module width_full">
    <form method = 'POST' action = ''>
        <fieldset>
            <label>Content</label>
            <textarea rows = 2 name = "message"  placeholder = 'Messages to Transmit via GCM'></textarea>
        </fieldset>
        <div class="clear"></div>
                </div>
            <footer>
                <div class="submit_link">
                    <input type="submit" name='submit' value="Send Push Notification" class="alt_btn">
                </div>
            </footer>
    </form>
    </article>

    </body>
</html>

首先将设备注册到GCM并获取GCM密钥。使用GCM密钥手动向该设备发送通知。这样做的请求是

curl --header "Authorization: key=" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -XPOST -d '{"data":{"New message through curl"},"registration_ids":["APA91bGe-Ihcjil6YXgLNEtf1fZCvcrgcn35rTOsHPrK7mxoz70TP6jmniChLXmIbLd642CWGo0fem9XrECa2ukvAvNYfNY7HcJ0JcxTInBxNn5Pml9YQIU6qwRVQ4v8GIE4oZv4vjoc"]}'
如果您使用此功能收到通知,则您的设备已成功注册

欲了解更多信息和测试的详细工作分类,请点击以下链接


客户是否收到通知?让我们试试var_dump$pushStatus、$val、$message我想我的代码有问题。但我不知道哪里有错。使用“msg”键在手机上检索消息……$message=array'msg'=>$pushMessage;