GCM 设置推送通知的到期日期
GCM set expiration date for push notifications
我正在使用 PHP 成功地为我的 android 用户发送 GCM 推送通知,但我想为我的推送通知设置到期日期,以防用户 1 周内没有互联网连接,它将不会收到旧的推送通知。
我该如何实现。
谢谢。
这是 php 代码:
private $GOOGLE_API_KEY = "XXXXX";
public function send($registration_id, $data) {
// include config
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_id,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $this->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;
}
在您的推送通知数据中发送日期和时间,并在创建通知之前在应用程序中检查该日期时间。
有一个参数 time_to_live
是您在发送时设置的。值以秒为单位(仅限一分钟以下)
$fields = array(
'registration_ids' => $registration_id,
'data' => $data,
'time_to_live' => 60
);
You can use the time_to_live parameter in the send request to specify the maximum lifespan of a message. The value of this parameter must be a duration from 0 to 2,419,200 seconds, and it corresponds to the maximum period of time for which GCM will store and try to deliver the message. Requests that don't contain this field default to the maximum period of 4 weeks.
更多here
我正在使用 PHP 成功地为我的 android 用户发送 GCM 推送通知,但我想为我的推送通知设置到期日期,以防用户 1 周内没有互联网连接,它将不会收到旧的推送通知。 我该如何实现。
谢谢。
这是 php 代码:
private $GOOGLE_API_KEY = "XXXXX";
public function send($registration_id, $data) {
// include config
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_id,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $this->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;
}
在您的推送通知数据中发送日期和时间,并在创建通知之前在应用程序中检查该日期时间。
有一个参数 time_to_live
是您在发送时设置的。值以秒为单位(仅限一分钟以下)
$fields = array(
'registration_ids' => $registration_id,
'data' => $data,
'time_to_live' => 60
);
You can use the time_to_live parameter in the send request to specify the maximum lifespan of a message. The value of this parameter must be a duration from 0 to 2,419,200 seconds, and it corresponds to the maximum period of time for which GCM will store and try to deliver the message. Requests that don't contain this field default to the maximum period of 4 weeks.
更多here