Search code examples
node.jsfirebasehttp-postfirebase-cloud-messagingfirebase-notifications

Firebase notification can't create device group through HTTP.post


I'm trying to create a device group as described in the documentation (https://firebase.google.com/docs/cloud-messaging/js/device-group) from a NodeJS backend but I can't make it, I'm always facing an 400 error

Anyone has an idea what I'm doing wrong?

const httpRequest = require('request');

const options = {
    url: 'https://fcm.googleapis.com/fcm/notification',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'key=AAA...vR',
        'project_id': '76...8'
    },
    body: JSON.stringify({
        operation: 'create',
        notification_key_name: 'my-unique-key-name',
        registration_ids: ['token1', 'token2']
    })
};

    httpRequest(options, (error, response, body) => {
    if (!error && response.statusCode === 200) {
        resolve(Converter.parseJSON(body));
    } else {
        reject(error);
    }
});

Thx in advance for any hints or help!


Solution

  • Turns out that I'm dumb. I printed out the all response:

    console.log(response);
    

    and discovered at the very end the reason why it didn't work:

     body: '{"error":"notification_key already exists"}' }
    

    so I just tried with another notification_key_name and it worked like a charm