Android向所有用户GCM发送通知

时间:2014-03-23 16:11:32

标签: php android

我想向使用GCM的5位用户等所有用户发送通知。这是我到目前为止所尝试的:

    <?php

    include 'db.php';

    $sql = "SELECT gcm_regid
        FROM gcm_users";
    $stmt = $conn->prepare($sql);
    $stmt->execute();

    $results = array();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
        $results[] = $row;
    }


    $message = "This is a multicast message sent to all users!";

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

    foreach($results as $value) {
      $fields = array(
            'registration_ids' => $value,
            '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;



?>

但我没有取得任何成功。有任何想法吗?非常感谢帮助。

0 个答案:

没有答案