即使使用不同的折叠键,Android GCM也会折叠


Android GCM collapse even if with different collapse keys

我正在使用这个lib发送两个不同的消息,它们具有不同的折叠键,但在我的设备上,我收到了第一个消息,然后第二个消息超过了第一个。

我想在设备上的Android通知标题中分别显示这两个。

为了记录在案,我正在使用这个Phonegap插件来接收推送通知。

这是我的代码:

    $gcmApiKey = 'api key here';
    $deviceRegistrationId = 'device regid here';
    $numberOfRetryAttempts = 5;
    $collapseKey = '1';
    $payloadData = ['title' => 'First Message Title', 'message' => 'First message'];
    $sender = new Sender($gcmApiKey);
    $message = new Message($collapseKey, $payloadData);
    $result = $sender->send($message, $deviceRegistrationId, $numberOfRetryAttempts);
    // Sending Second message
    $collapseKey = '2';
    $payloadData = ['title' => 'Second Message Title', 'message' => 'Second Message'];
    $sender = new Sender($gcmApiKey);
    $message = new Message($collapseKey, $payloadData);
    $result = $sender->send($message, $deviceRegistrationId, $numberOfRetryAttempts);

如果我理解得对,您的问题是第一个通知在显示后被第二个通知取代。

如果是这样的话,那么您的错误不在PHP方面,而是在Java代码中。

如果您显示通知,则调用此方法:

NotificationManager.notify(int id, Notification notification)

最有可能的情况是,每次调用此方法时都将id参数设置为相同的值。

id的效果是,系统将只显示一个具有相同ID的通知——最新的。使用与以前相同的id的典型用例是更新以前的通知。

如果要显示多个通知,则需要每次设置不同的id。你可以使用一个随机数,或者更好的是使用之前定义的内容ID。

GCM折叠键具有不同的效果:

当您定义折叠密钥时,当多条消息在GCM服务器中为同一用户排队时,只会传递具有任何给定折叠密钥的最后一条消息。

这意味着,例如,如果你的手机关机,你只会收到一条带有相同折叠键的信息。如果你的手机在发送第二个通知之前收到了第一个通知,它不会有任何作用。

使用PhoneGap插件设置

该插件有一个非常混乱的文档,但如果我们查看源代码,我们会发现这个未记录的功能:

int notId = 0;
try {
    notId = Integer.parseInt(extras.getString("notId"));
}
mNotificationManager.notify((String) appName, notId, mBuilder.build());

这意味着,如果您将有效载荷更改为,例如:

$payloadData = ['title' => 'First Message Title', 'message' => 'First message', 'notId' => mt_rand()];

您的通知不会相互替换。