无法通过GCM发送消息

时间:2013-01-20 11:02:37

标签: php android ajax google-cloud-messaging

我正在使用PHP脚本通过GCM将消息推送到Android设备上。我有一个html表单,接收文本输入作为消息,然后将此消息推送到Android设备。

现在,当我尝试使用两个单独的字段时1.摘录2.消息 我的Android设备收到一条空消息。

我需要知道我哪里出错了。

这是我的HTML表单

                    <form id="<?php echo $row["id"] ?>" name="" method="post" onsubmit="return sendPushNotification('<?php echo $row["id"] ?>')"> 
                        <label>User:</label> <span><?php echo $row["id"] ?></span> 
                        <div class="clear"></div> 
                        <div class="send_container"> 
                            <textarea rows="3" name="excerpt" cols="10" class="txt_excerpt" placeholder="Type excerpt here"></textarea>
                            <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> 

这是ajax脚本

<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("");
                  $('.txt_excerpt').val("");    
            }, 
            error: function(xhr, textStatus, errorThrown) { 

            } 
        }); 
        return false; 
    } 
</script> 

这是Send_message.php代码

if (isset($_GET["regId"]) && isset($_GET["message"]) && isset($_GET["excerpt"])) { 
    $regId = $_GET["regId"]; 
    $message = $_GET["message"];
    $excerpt = $_GET["excerpt"];

    include_once './GCM.php'; 

    $gcm = new GCM(); 

    $registatoin_ids = array($regId); 
    $message = array("news" => $message, "exc" => $excerpt); 


    $result = $gcm->send_notification($registatoin_ids, $message); 

    echo $result; 

这是GCM.php代码

public function send_notification($registatoin_ids, $message, $excerpt) {
        // 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;
    }

这是android GCMIntentService代码

@Override
protected void onMessage(Context context, Intent intent) { 
    Log.i(TAG, "Received message"); 
    String message = intent.getExtras().getString("news");
    String excerpt = intent.getExtras().getString("exc");
    Log.i("Received ->", excerpt+" "+message);
    if(message!=null)
    {
        displayMessage(context, message); 
    // notifies user 
        generateNotification(context, message); 
    }
}

每当我从服务器推送消息时,我在设备上看到的所有内容都是空白通知。

0 个答案:

没有答案