使用GCM发送多条消息

时间:2014-01-21 07:06:32

标签: php android push-notification google-cloud-messaging

我从2天开始就处理这个问题了。我想要做的是,我已经向注册的GCM设备发送了多条消息。直到现在我可以向设备发送单个消息。以下是发送消息的代码。

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

接收方正收到此消息。 //这个代码在GCM.php

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

但我想在单个通知中发送多条消息。为此我做了...

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

?>

在android中我编写了接收这样的消息的函数......

@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);
}

但是我得到了#34;价格&#34;并获得&#34;额外&#34;的结果。 如何在单个通知中收到多个消息字符串?

4 个答案:

答案 0 :(得分:2)

在send_message.php

将两封邮件放在同一个对象中。我不知道你为什么称之为price,但尝试这样:

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

在GCM.php中

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

在您的Android服务中

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

答案 1 :(得分:2)

您在JSON中传递的所有有效内容参数都应在data元素内。

您的JSON应如下所示:

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

而不是这样:

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

答案 2 :(得分:1)

您可以编写包含多条消息的JSON响应

然后你在android中获得JSON,解析它并得到你的多个消息。

答案 3 :(得分:0)

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端

/**
     * 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);
    }
相关问题