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 208 forks source link

How to add analytics label to message? #340

Closed gpkamp closed 4 years ago

gpkamp commented 4 years ago

My push notifications are being sent ok but not appearing in the Firebase Cloud Messaging logs. Their documentation mentions that you should add an analytics label. https://firebase.google.com/docs/cloud-messaging/understand-delivery?authuser=0#adding_analytics_labels_to_messages How do I do this with node-gcm?

eladnava commented 4 years ago

Hi @gpkamp, As per the FCM docs, set the fcm_options.analytics_label param using the following code:

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' },
    fcm_options: {
        analytics_label: 'call-notification'
    }
});

// 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);
});
ltnilaysahu commented 3 years ago

I tried with 1.0.3 , it didnt work

`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('.....');

// Prepare a message to be sent var message = new gcm.Message({ notification: { title: 'Title of your push notification', body: 'Body of your push notification' }, data: { key1: 'msg1' }, fcm_options: { analytics_label: 'call-notification' } });

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

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

eladnava commented 3 years ago

Hi @ltnilaysahu, When debugging node-gcm, I could confirm that using your exact sample code, the fcm_options.analytics_label is passed in the request to the FCM API:

Screen Shot 2020-07-24 at 10 59 59 AM

Therefore, there is no bug in node-gcm. Instead, check for a delay in the analytics being posted to the Firebase dashboard, or using the wrong label ID.

ltnilaysahu commented 3 years ago

Hi @eladnava . Correct its getting passed to the FCM API that node-gcm is using but according to the docs Labeling messages is very useful for custom analysis, allowing you to filter delivery statistics by labels or sets of labels. You can add a label to any message sent via the HTTP v1 API by setting the fcmOptions.analyticsLabel field in the message object, or in the platform-specific AndroidFcmOptions or ApnsFcmOptions fields. And i believe node-gcm is not using HTTP v1 API which is POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send as per https://firebase.google.com/docs/cloud-messaging/migrate-v1

eladnava commented 3 years ago

Hi @ltnilaysahu, Nice catch. Indeed this parameter is not supported in the FCM Legacy HTTP Server API.

Currently, node-gcm is specifically built to support the legacy API format and there are no current plans to migrate to the FCM HTTP v1 API.

I'd recommend checking out the official firebase package to send notifications.

You can then add an analytics label by setting the fcm_options.analytics_label field in the message object: https://firebase.google.com/docs/cloud-messaging/send-message