[英]GCM non-collapsible message always replaces the previous message
我正在尝试通过Google Cloud Messaging发送不折叠的消息。 由于某种原因,新消息始终会替换前一条消息。 我尝试使用不同的折叠键无效。 同时忽略折叠键也不起作用。 可能是什么问题呢?
这是一个示例代码:
<?php
$ids[] = '<notification registration id of the test phone>';
sendNotification($ids, "test message 1", "key1");
sendNotification($ids, "test message 2", "key2");
function sendNotification($ids, $message, $collapseKey)
{
$apiKey = '<api key here>';
$url = 'https://android.googleapis.com/gcm/send';
$data['title'] = 'AppName';
$data['message'] = $message;
$post['registration_ids'] = $ids;
$post['data'] = $data;
if ($collapseKey) {
$post['collapse_key'] = $collapseKey;
}
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
$result = curl_exec( $ch );
if ( curl_errno( $ch ) )
{
echo 'GCM error: ' . curl_error( $ch );
}
curl_close( $ch );
echo $result;
}
?>
从GCM折叠键文档中 ,省略折叠键应可防止替换旧消息。 除非您的某些消息已过期或未连接设备。
此外,带有notify方法调用的NOTIFICATION_ID是在客户端应用程序中实现的,而不是在服务端代码中实现的。 因此,如果您使用的是HTTP服务器,则没有关系。 您可以在客户端应用程序的广播接收器中参考有关NOTIFICATION_ID的此文档 。 此StackOverflow答案还涉及如何生成唯一的NOTIFICATION_ID。
我要回答我自己的问题。
有两种情况:
例如:您在手机离线时发送两条消息(没有wifi,没有数据,已关闭)
后台/已终止运行的应用-我只收到一个通知,但实际上我收到了两个数据消息
前景-我收到两个通知
请记住不要添加“ collapse_key”参数。
希望这可以帮助其他遇到相同问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.