我正在做一个使用gcm的Android应用程序。我必须在服务器端设置,因为消息应该在一天后删除,如果没有交付。请帮助我。以下是向gcm服务器发送通知的功能
public function send_notification($registatoin_ids, $message, $coll_key) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'collapse_key' => $coll_key,
'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;
}
答案 0 :(得分:0)
只需添加time_to_live
参数,并为其指定一个值,该值等于您希望将消息存储在GCM服务器中的最大秒数,而不是交付。
$fields = array(
'collapse_key' => $coll_key,
'registration_ids' => $registatoin_ids,
'data' => $message,
'time_to_live' => $time_to_live,
);