André Policarpo
André Policarpo

Reputation: 31

Send android notification with node-gcm without collapsing them

I'm sending notifications to an android device using node-gcm, and in some cases, I need to send an image with the notification to display as a thumbnail, like so: notification with thumbnail

And sometimes I need to send notifications without the thumbnail. With the below code I can send the image in the notification, the problem is when another notification is received, they collapse, making the new one overwriting a notification that was already there:

    var message = new gcm.Message({
                "data" : {
                    "title" : "Test",
                    "message" : "Test message!",
                    "priority" : 2, // Highest priority.
                    "ledColor" : [255, 0, 0, 1],
                    "content-available": "1",
                    "image": req.body.notificationImageUrl, //<-- image URL
                },
            });

But if I set up the message like below, I can't find a way to send the image but the notifications do not collapse and all of them appear. Another problem is that the led does not activate in this version:

    var message = new gcm.Message({
                data: {
                    "priority" : 2, // Highest priority.
                    "content-available": "1",
                    "image": req.body.notificationImageUrl,  // <-- image URL
                },
                notification:{
                    title: "Test",
                    body: "Test message!",
                    color: "#892121",
                    sound: 'default',
                    vibrationPattern: [300, 150, 300], // Vibrate for 300ms then wait 150ms and then vibrate for 300ms.
                    ledColor: [255, 0, 0, 1], // <-- this does not work
                },
            });

What I want is a way to send the notification with the thumbnail in a way that it does not overwrite a notification that was previously there.

EDIT: I forgot to mention, im using Ionic Framework with Cordova so I can't manage the notifications in the device unless there is a way to do it through those frameworks. Thanks in advance!

Upvotes: 1

Views: 797

Answers (2)

Andr&#233; Policarpo
Andr&#233; Policarpo

Reputation: 31

While going through the node-gcm issues I found the solution, there is a parameter called "notId" to add a ID to a notification.

 var message = new gcm.Message({
     "data": {
         "title": "Test",
         "message": "Test message!",
         "priority": 2, // Highest priority.
         "ledColor": [255, 0, 0, 1],
         "content-available": "1",
         "image": req.body.notificationImageUrl,
         "notId": parseInt(Math.random() * new Date().getSeconds(), 10), // <-- this solved the problem
     },
 });

Upvotes: 2

Benjamin Diaz
Benjamin Diaz

Reputation: 196

The management of the display of notifications is done on the device. You have to make sure you are setting, on Android, a different NOTIFICATION_ID for each notification, or the last one will always replace the one before it.

// Sets an ID for the notification - make sure to change this for different notifications
int mNotificationId = 001;

// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Builds the notification and issues it
mNotifyMgr.notify(mNotificationId, mBuilder.build());

Upvotes: 0

Related Questions