ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 206 forks source link

Data vs Display notifications #324

Open 0x62 opened 6 years ago

0x62 commented 6 years ago

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?

eladnava commented 6 years ago

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);
});