我正在使用 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 折叠键有不同的效果:
这意味着,例如,如果您的手机处于关机状态,您将只会收到一条具有相同折叠键的消息。如果您的手机在您发送第二个通知之前收到第一个通知,它不会执行任何操作。
用你的 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/