Abhilasha
Abhilasha

Reputation: 1225

How to send message to multiple android devices using FCM in Node js?

I tried sending message to single device i.e. to single Registration id and it worked fine but when tried to add multiple Registration Ids it gives 'InvalidServerResponse' error. e.g. Works for regTokens = 'regId1'; But doesn't work for regTokens = ['regId1','regId2'];

var FCM = require('fcm-node');
// Add API Key
var fcm = new FCM('<server-key>');

exports.sendMessage = function (regTokens, messageToSend, callback) {
  var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
      to: regTokens,

      data: { 
        ar_message: messageToSend
      }
  };

    fcm.send(message, function(err, response){
        if (err) {
            console.log("Something has gone wrong!",err);
        } else {
            console.log("Successfully sent with response: ", response);
        }
        callback(err, 'Success');
      });
}

Upvotes: 5

Views: 12898

Answers (4)

Dipak Raut
Dipak Raut

Reputation: 3

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

send_fcm_notifications()
function send_fcm_notifications(){ 

// notification object with title and text

var notification = {
  'title': 'Best Deals',
  'text': 'Mobile Devices at 50% off. Only for today'
};

// fcm device tokens array

var fcm_tokens = ["Key 1", "Key 2"]

var notification_body = {
    'notification': notification,
          'registration_ids': fcm_tokens
  }


fetch('https://fcm.googleapis.com/fcm/send', {

        'method': 'POST',

        'headers': {
          // replace authorization key with your key
          'Authorization': 'key=' + 'Authorization Key',
          'Content-Type': 'application/json'
        },

         'body': JSON.stringify(notification_body)
      })
      .then(function(response) {
        console.log(response);
      })
      .catch(function(error) 
        console.error(error);
      })
}

Upvotes: 0

vishal sharma
vishal sharma

Reputation: 49

var message = {
            // to : "token", //for single device
            registration_ids: ["token1", "token2"], // for Multiple device
            collapse_key: 'your_collapse_key',
            notification: notify,
            data: payload,
         };

Upvotes: 0

jackycflau
jackycflau

Reputation: 1121

An update for this thread: Use admin.messaging.Messaging.sendToDevice() to send messages to multiple android devices.

https://firebase.google.com/docs/reference/admin/node/admin.messaging.Messaging#sendToDevice

messaging.sendToDevice(registrationTokens, payload, options)

  • registrationTokens: Array of String (Tokens of the recipients)

  • payload: Message payload

  • options: (Optional) admin.messaging.MessagingOptions

Upvotes: 4

AL.
AL.

Reputation: 37778

Update: For v1, it seems that registration_ids is no longer supported. It is strongly suggested that topics be used instead.


When sending to specified multiple registration tokens, you must use registration_ids instead of to. From the docs (emphasis mine):

This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.

The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 registration tokens. To send a message to a single device, use the to parameter.

Multicast messages are only allowed using the HTTP JSON format.

var message = {
    registration_ids: regTokens,

   data: { 
        ar_message: messageToSend
   }
  };

Upvotes: 12

Related Questions