node-gcm icon indicating copy to clipboard operation
node-gcm copied to clipboard

Data vs Display notifications

Open 0x62 opened this issue 7 years ago • 1 comments

In this SO post they discuss the difference between data and display messages (the main difference being data messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed).

I've been trying to use this library (through node-pushnotifications) but it appears the messages are being sent as display notifications. How do I change the type to data so the callback will be triggered even if the app is in the background?

0x62 avatar Aug 03 '18 08:08 0x62

Hi @0x62, Using this sample code only a data payload is sent and not a notification display payload:

var gcm = require('node-gcm');

// Set up the sender with your GCM/FCM API key (declare this once for multiple messages)
var sender = new gcm.Sender('YOUR_API_KEY_HERE');

// Prepare a message to be sent
var message = new gcm.Message({
    data: { key1: 'msg1' }
});

// Specify which registration IDs to deliver the message to
var regTokens = ['YOUR_REG_TOKEN_HERE'];

// Actually send the message
sender.send(message, { registrationTokens: regTokens }, function (err, response) {
	if (err) console.error(err);
	else console.log(response);
});

eladnava avatar Aug 03 '18 09:08 eladnava