5

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');
      });
}
Abhilasha
  • 1,177
  • 4
  • 10
  • 17
  • It works for both Registration Ids if sent individually. But doesn't work for array. – Abhilasha Feb 22 '17 at 12:45
  • 1
    Please make sure the [code is self-contained](http://stackoverflow.com/help/mcve). Right now, we have no idea what `regTokens` is, and that's likely quite crucial to why it fails. – Frank van Puffelen Feb 22 '17 at 15:31

4 Answers4

12

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
   }
  };
Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
4

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

jackycflau
  • 1,101
  • 1
  • 13
  • 33
0
var message = {
            // to : "token", //for single device
            registration_ids: ["token1", "token2"], // for Multiple device
            collapse_key: 'your_collapse_key',
            notification: notify,
            data: payload,
         };
0
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);
      })
}
David Buck
  • 3,752
  • 35
  • 31
  • 35