将推送消息从url发送到设备GCM

时间:2014-05-30 10:23:36

标签: php android push google-cloud-messaging

我发现了一个通过GCM发送推送消息的优秀代码。 问题是,是否可以更改String" Google Cloud Messaging运行良好"

地址栏示例我会写www.example.com/index.php?text=work

字符串将工作

由于

<?php
define("GOOGLE_API_KEY", "AIzaSyCJiVkatisdQ44rEM353PFGbia29mBVscA");
define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");

function send_gcm_notify($reg_id, $message) {
    $fields = array(
        'registration_ids'  => array( $reg_id ),
        'data'              => array( "message" => $message ),
    );

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

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Problem occurred: ' . curl_error($ch));
    }

    curl_close($ch);
    echo $result;
}

$reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
$msg = "Google Cloud Messaging working well";

send_gcm_notify($reg_id, $msg);

1 个答案:

答案 0 :(得分:1)

替换

"Google Cloud Messaging working well";

$_GET['text'];

使用更好的代码

filter_input (INPUT_GET, 'text', FILTER_SANITIZE_STRING);