设备上有时未收到 gcm
gcm not received on a device sometime
我正在使用 gcm 服务发送通知,它大部分时间都工作正常,但有时它不会向设备发送 gcm 或它发送消息真的很晚。我该如何解决这个问题?
代码:
/**
* 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;
}
我知道改变行为的唯一方法是将 delay_while_idle 设置为 true 或 false 并更改 time_to_live。例如通过设置 delay_while_idle = false,time_to_leave = 0 Google 将尝试尽快传递消息,但是 ttl=0 也意味着不能立即发送的消息(比方说用户在网络外的区域)很可能会丢失。
我在我的应用程序中所做的是发送 2 条具有相同 ID 的消息。一个 ttl=0,因此它尝试立即传送,另一个 ttl=20 分钟。这样,即使迟到,其中一些消息也会被传递。我不确定这对你的情况是否有意义。
delay_while_idle
的值必须设置为 true。从这个 documentation,它指出如果设备已连接但空闲,消息仍将立即传递,除非 delay_while_idle 标志设置为 true。
这也可能是 TCP 超时,Android 有一种机制可以每隔 x 分钟发送一个小网络数据包(称为心跳)以避免 tcp 连接超时并检查连接是否有效。您可以查看此 forum 来解释问题。
我正在使用 gcm 服务发送通知,它大部分时间都工作正常,但有时它不会向设备发送 gcm 或它发送消息真的很晚。我该如何解决这个问题?
代码:
/**
* 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;
}
我知道改变行为的唯一方法是将 delay_while_idle 设置为 true 或 false 并更改 time_to_live。例如通过设置 delay_while_idle = false,time_to_leave = 0 Google 将尝试尽快传递消息,但是 ttl=0 也意味着不能立即发送的消息(比方说用户在网络外的区域)很可能会丢失。
我在我的应用程序中所做的是发送 2 条具有相同 ID 的消息。一个 ttl=0,因此它尝试立即传送,另一个 ttl=20 分钟。这样,即使迟到,其中一些消息也会被传递。我不确定这对你的情况是否有意义。
delay_while_idle
的值必须设置为 true。从这个 documentation,它指出如果设备已连接但空闲,消息仍将立即传递,除非 delay_while_idle 标志设置为 true。
这也可能是 TCP 超时,Android 有一种机制可以每隔 x 分钟发送一个小网络数据包(称为心跳)以避免 tcp 连接超时并检查连接是否有效。您可以查看此 forum 来解释问题。