kotti Anouar
kotti Anouar

Reputation: 43

Sending FCM push in Node

I've got problem with sending message to Firebase Cloud Messaging by Node application.

 request({
        url: 'https://fcm.googleapis.com/fcm/send',
        method: 'POST',
        headers: {
          'Content-Type' :' application/json',
          'Authorization': 'key=AIzaSyAF9cvEThRo3ZlWCLuSU5k6W9kk0uumkLM',
          'project_id': '83933810320'
        },
        body: JSON.stringify(
          { "data": {'notification': notification, _id: _id, action: action},
            "registration_ids" : registration_ids,
            "content_available": true,
          }
        )
      }, function(error, response, body) {
        if (error) { 
          console.error(error, response, body); 
        }
        else if (response.statusCode >= 400) { 
          console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage+'\n'+body); 
        }
        else {
          console.log(response);
        }
      });

When I call the above code, it gives me response as 401 Unauthorized. I am not able to understand why I am getting this error. I have used proper server key. Is there any syntax error or anything wrong in the strategy used by me.

Upvotes: 4

Views: 8038

Answers (2)

Orlando
Orlando

Reputation: 9692

You need to use the Cloud Messaging key, is different from the other API keys.

To get the key go to https://console.firebase.google.com/project/:project-name/settings/cloudmessaging (replace :project_name with your project name)

Upvotes: 1

Libu Mathew
Libu Mathew

Reputation: 2996

You can send fcm message from web application to either mobile device or to a topic.

Sending fcm to a device : https://firebase.google.com/docs/cloud-messaging/send-message#send_messages_to_specific_devices

Sending fcm to a topic : https://firebase.google.com/docs/cloud-messaging/send-message#http_post_request

Upvotes: 1

Related Questions