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

Php 即使使用不同的折叠键,Android GCM也会折叠,php,cordova,google-cloud-messaging,Php,Cordova,Google Cloud Messaging,我使用不同的折叠键发送两条不同的消息,但在我的设备上,我接收到第一条消息,然后第二条消息通过第一条消息发送过来 我想在设备上的Android通知标题中分别显示这两个 对于我用来接收推送通知的记录 这是我的密码: $gcmApiKey = 'api key here'; $deviceRegistrationId = 'device regid here'; $numberOfRetryAttempts = 5; $collapseKey = '1'; $

我使用不同的折叠键发送两条不同的消息,但在我的设备上,我接收到第一条消息,然后第二条消息通过第一条消息发送过来

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

对于我用来接收推送通知的记录

这是我的密码:

    $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()];

您的通知不会相互替换。

除了在服务器端执行的操作外,您还需要在Android应用程序中进行一些配置。是的,您理解,您说的没错,但是。。。我没有使用Java,我使用的是HTML5和CordovaBro,我稍后会尝试并接受您的答案。。谢谢