php - 对 2 种类型的消息 gcm 使用折叠键

标签 php android google-cloud-messaging

在我的 gcm android 应用程序中,我从应用程序服务器发送两种类型的消息。我知道什么是折叠键,但我不知道如何使用。这些是两种类型的消息。 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;
    }

}

?>

最佳答案

您应该将 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 中的数据类型进行初始化。

关于php - 对 2 种类型的消息 gcm 使用折叠键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683357/

相关文章:

events - 移动和桌面设备的实时消息服务

php - foreach($ex as $k=>$v) 中的 $k => $v 是什么意思?

android - 为我的 android 应用程序提供一个用 sqlcipher 加密的数据库不起作用,无法打开它

java - HttpTransportSE .call() 方法没有任何 Action

Android GPS networkProvider 不工作

google-cloud-messaging - Google Developer Console 的凭证端许可

ios - 将 GCM sdk 包含到 cocoapod 项目中

php - 'simplexml' php 到 mysql 数据库循环只返回第一个元素

php - 混合身份验证 Codeigniter 说“验证用户时出错。”在我托管到服务器后。本地服务器工作正常

php - 正则表达式帮助 - Wordpress (search-regex)