4

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.

kotti Anouar
  • 43
  • 1
  • 4
  • have you checked if your server API key is valid?. I had the same issue today and my API key was different in the Firebase web site under Cloud Messaging. So I had to change the API key again. – GeorgeLBA Aug 24 '16 at 18:02
  • I corrected the problem I created an android application in the firebase "Add Firebase to your Android app" and I copied api_key exist in google-service.json yesterday it works but not today !! i resolved this problem by cklick in "Add Firebase to your web app" and i copy apiKey apiKey it's not same api_key in google-service.json – kotti Anouar Aug 24 '16 at 18:09
  • Try this: https://github.com/emretekince/fcm-web-push – Emre Tekince Sep 09 '17 at 15:12

2 Answers2

1

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

Libu Mathew
  • 2,976
  • 23
  • 30
1

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)

Orlando
  • 9,374
  • 3
  • 56
  • 53