Android GCM向设备发送多条消息

时间:2014-03-02 17:33:52

标签: php android google-cloud-messaging

我有点困在一个问题上。我需要向设备发送消息通知和警报通知。当用户向其他人发送消息时发送消息通知,并生成警报通知以从服务器提醒用户。这意味着我需要多种通知类型。我在我的服务器上使用它将通知推送到我的设备。

// This is code from the class that take user input
$notify = "This is message"
$msg = array("Message" => $notify);
$sense = $gcm->send_notification($registatoin_ids, $msg);

//This is the send notification in GCM class
public function send_notification($registatoin_ids, $message) {
    // 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;
}

如何调整上述代码以满足我的要求。我尝试发送

$fields = array(

    "registration_ids" : registatoin_ids,
    "data" : {
        "type" : "Message",
        "Message" : $message,
    }

     );

但是我得到了错误

 E/JSON(3444): <b>Parse error</b>:  syntax error, unexpected ':', expecting ')' in <b>/home/a8709494/public_html/mobile/GCM.php</b> on line <b>25</b><br />

1 个答案:

答案 0 :(得分:2)

您需要使用=>运算符:

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