gpt4 book ai didi

php - 即使使用不同的折叠键,Android GCM 也会崩溃

转载 作者:可可西里 更新时间:2023-10-31 22:10:56 25 4
gpt4 key购买 nike

我正在使用 this lib用不同的折叠键发送两条不同的消息,但在我的设备上,我收到了第一条消息,然后第二条消息从第一条消息过来。

我想在设备上的 Android 通知 header 中分别显示这两者。

郑重声明,我使用的是 this Phonegap plugin接收推送通知。

这是我的代码:

    $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 折叠键有不同的效果:

When you define a collapse key, when multiple messages are queued up in the GCM servers for the same user, only the last one with any given collapse key is delivered.

这意味着,例如,如果您的手机处于关机状态,您将只会收到一条具有相同折叠键的消息。如果您的手机在您发送第二个通知之前收到第一个通知,它不会执行任何操作。

用你的 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()];

您的通知不会相互替换。

关于php - 即使使用不同的折叠键,Android GCM 也会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29235371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com