Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GCM collapse even if with different collapse keys

I'm using this lib to send two different messages with different collapse keys, but on my device I'm receiving the first and then the second is coming over the first.

I would like to have the two separately in the Android notification header on device.

For the record I'm using this Phonegap plugin to receive the push notification.

Here is my code:

    $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);
like image 817
Daniel Faria Avatar asked Mar 16 '23 15:03

Daniel Faria


1 Answers

If I understand you right, your problem is that the first notification is replaced by the second after it was shown.

If that is the case, your mistake is not on the PHP-side here, but in your Java-code.

If you show a notification you call this method:

NotificationManager.notify(int id, Notification notification)

Most likely, you are setting the id parameter to the same value each time you call this method.

The effect of the id is that the system will only show one notification with the same ID - the newest. A typical use-case to use the same id as before would be to update a previous notification.

If you want to display multiple notifications, you need to set a different id each time. You could use a random number or better yet use a previously defined ID of your content.

The GCM collapse key has a different effect:

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.

That means, for example, if your phone was off, you would only receive one message with the same collapse key. It doesn't do anything if your phone receives the first notification before you send the second.

To set it with your PhoneGap plugin

The plugin has a really messy documentation but if we look into the source code, we'll find this undocumented feature:

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

That means, if you change your payload to, for example:

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

your notifications won't replace each other.

like image 139
Johann Bauer Avatar answered Mar 24 '23 15:03

Johann Bauer