使用折叠键2种消息gcm

时间:2014-03-27 09:33:16

标签: php android google-cloud-messaging

在我的gcm安卓应用程序中,我从应用程序服务器发送了两种类型的消息。我知道什么是折叠键,但是我不知道如何使用。这些是两种类型的消息。 1。

$message = array(
        "price" => "signal",
        "type" => $user_type,
        "date" => $date1,
        "name" => $signal_name,
        "buy" => $price,
        "stop" => $stop,
        "tv" => $trig_value,
        "tp" => $profit,       
        "res" => $result,
    );

第二个

$message = array(
      "price" => "instru",
      "price1" => $trade1,
       "price2" => "$trade2",
      "price3" => "$trade3",
      "price4" => "$trade4",
      "price5" => "$date"
   );

我需要的是为gcm服务器中保留的两种消息类型发送的最后消息。我怎么能这样做。我也给gcm类。请帮助。

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

}

?>

1 个答案:

答案 0 :(得分:1)

您应该将collapse_key参数添加到JSON。

JSON应如下所示:

例如,对于第一种类型:

{ 
  "registration_ids":["...", "..."],
  "collapse_key": "type1",
  "data": {
        "price" => "...",
        "type" => "...",
        ...
  },
}

对于第二种类型,请为collapse_key提供不同的值。

根据您的代码和我对PHP的有限知识,您需要这样的东西:

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

$collapse_key应根据$message中的数据类型进行初始化。