Looking Forward
Looking Forward

Reputation: 3585

Send multiple messages with GCM

I've been dealing with this issue since 2 days. What I want to do is, I've to send multiple messages to registered GCM device. Till now I can send single message to device. Below is the code to send message.

send_message.php

<?php

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

        include_once './GCM.php';

        $gcm = new GCM();

        $registatoin_ids = array($regId);
        $message = array("price" => $message);

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

    }

GCM.php

<?php

class GCM {

    //put your code here
    // constructor
    function __construct() {

    }


    //Sending Push Notification

    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once './config.php';


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

        $fields = array(
            'registration_ids' => $registatoin_ids,
        'message' => $message, 
          );


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

        //print_r($headers); exit();
        // 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;
    }
}
?>

Here message is being received on receiver side.. // code for this is in GCM.php

 $fields = array(
            'registration_ids' => $registatoin_ids,
            'message' => $message, 
          );

But I want to send multiple messages in single notification. for that what I did...

send_message.php

<?php


if (isset($_REQUEST["regId"]) && isset($_REQUEST["message"]) && isset($_REQUEST["data"])) {
    $regId = $_REQUEST["regId"];
    $message = $_REQUEST["message"];
    $data = $_REQUEST["data"]; //added third parameter


    include_once './GCM.php';

    $gcm = new GCM();

    $registatoin_ids = array($regId);
    $message = array("price" => $message);
    $data = array("extra" => $data);


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

GCM.php

<?php

class GCM {

    //put your code here
    // constructor
    function __construct() {

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message, $data) {
        // include config
        include_once './config.php';


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

        $fields = array(
            'registration_ids' => $registatoin_ids,
        'message' => $message, 
        'data' => $data,
        );


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

        //print_r($headers); exit();
        // 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;
    }
}

?>

And in android I wrote the function to receive message like this...

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getStringExtra("price");
    String newmessage = intent.getStringExtra("extra");
    displayMessage(context, message + newmessage);
    generateNotification(context, message + newmessage);
}

But I'm getting null result for "price" and getting result for "extra". How can I receive multiple message string in single notification?

Upvotes: 0

Views: 4872

Answers (4)

Looking Forward
Looking Forward

Reputation: 3585

send_message.php

<?php

if (isset($_REQUEST["regId"]) && isset($_REQUEST["message"]) && isset($_REQUEST["extra"])) {
    $regId = $_REQUEST["regId"];
    $message = $_REQUEST["message"];
    $extra= $_REQUEST["extra"];


    include_once './GCM.php';

    $gcm = new GCM();

    $registatoin_ids = array($regId);
    $message = array("message" => $message, "extra" => $extra);

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


}

GCM.php

<?php

class GCM {

    function __construct() {

    }

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

        //print_r($headers); exit();
        // 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';
        echo json_encode($fields);
        echo $result;
    }

}

Android side

/**
     * Method called on Receiving a new message
     * */
    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        String message =  intent.getExtras().getString("extra");
        String newmessage = intent.getExtras().getString("message");
        displayMessage(context, message + newmessage);
        generateNotification(context, message + newmessage);
    }

Upvotes: 0

meda
meda

Reputation: 45500

In send_message.php

Put both messages in the same object. I don't know why you call it price, but try like this:

$message = array('message' => $message,
                 'extra' => $data
);

In GCM.php

$fields = array(
    'registration_ids' => $registatoin_ids,
    'data' => $message,
);

In your Android Service

protected void onMessage(Context context, Intent intent) {
    //log the message in JSON format
    Log.i(TAG, "Received message >> " + intent.getExtras().toString());
    //Retrieve message and extra
    String message = intent.getExtras().getString("message");
    String newmessage = intent.getExtras().getString("extra");
    //Now display the message
    displayMessage(context, message + newmessage);
    generateNotification(context, message + newmessage);
}

Upvotes: 2

Eran
Eran

Reputation: 394156

All the payload parameters you pass in your JSON should be within the data element.

Your JSON should look like this :

{
  "registration_ids":["xxx", "yyy"],
  "data": {
    "price": "price value",
    "extra": "extra value"
  }
}

And not like this :

{
  "registration_ids":["xxx", "yyy"],
  "message": {
    "price": "price value"
  },
  "data": {
    "extra": "extra value"
  }
}

Upvotes: 2

user957654
user957654

Reputation:

you can write a JSON response that contain a multiple messages

and the you get the JSON in android , parse it and get your multiple Messages .

Upvotes: 1

Related Questions