Nepal12
Nepal12

Reputation: 593

Android GCM send multiple message to device

I kinda stuck on a problem. I need to send message notification and alert notification to the device. Message notification is sent when a user send message to other and alert notification is generated to alert user from server. That mean I need multiple notification types. I am using this on my server to push notification to my device.

// 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;
}

How can I adjust the above code to meet my requirement. I tried by sending

$fields = array(

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

     );

But I got error as

 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 />

Upvotes: 0

Views: 979

Answers (1)

nKn
nKn

Reputation: 13761

You need to use the => operator for this:

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

Upvotes: 2

Related Questions