Php 对2种类型的消息gcm使用折叠键
在我的gcm android应用程序中,我从应用程序服务器发送了两种类型的消息。我知道什么是折叠键,但我不知道如何使用。这两种类型的消息。 一, 第二个Php 对2种类型的消息gcm使用折叠键,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,在我的gcm android应用程序中,我从应用程序服务器发送了两种类型的消息。我知道什么是折叠键,但我不知道如何使用。这两种类型的消息。 一, 第二个 $message = array( "price" => "instru", "price1" => $trade1, "price2" => "$trade2", "price3" => "$trade3", "price4" => "$trade4"
$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;
}
}
?>
您应该将
折叠键
参数添加到JSON中
JSON应该如下所示:
$fields = array(
'collapse_key' => $collapse_key,
'registration_ids' => $registatoin_ids,
'data' => $message,
);
例如,对于第一种类型:
{
"registration_ids":["...", "..."],
"collapse_key": "type1",
"data": {
"price" => "...",
"type" => "...",
...
},
}
对于第二种类型,请为折叠\u键
指定不同的值
基于您的代码和我对PHP的有限知识,您需要以下内容:
$fields = array(
'collapse_key' => $collapse_key,
'registration_ids' => $registatoin_ids,
'data' => $message,
);
应根据$message
中的数据类型初始化$collapse\u键