0

I use the following C# web method to webRequest Firebase Cloud Messaging

public void messgaeFCM(String cToken) {
    try
    {


    //send message to clientToken through FCM server
    WebRequest wr = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
    wr.Method = "POST";
    wr.ContentType = "application/json";
    wr.Headers.Add("Authorization", "Key='" + FIREBASE_PROJECT_ID +"'");
    String notification = "{ 'data': " +
            "{" +
            "'title': 'Firebase notification'," +
            "'detail': 'I am firebase notification. you can customise me. enjoy'," +
            "}," +
            "'to' : '" + cToken + "'" +
            "}";
    byte[] byteArray = Encoding.UTF8.GetBytes(notification);

    Stream dataStream = wr.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    WebResponse response = wr.GetResponse();

    }
    catch (Exception e)
    {
        new general().logError(e.Message);
    }
}

FIREBASE_PROJECT_ID value set to the FCM project id.

cToken value set for a valid client token.

The above code always returns the following error :

The remote server returned an error: (401) Unauthorized.

Not sure what is wrong...

KENdi
  • 7,576
  • 2
  • 16
  • 31
Shai
  • 355
  • 2
  • 5
  • 18
  • You may need to encode the Authorization value? Are you sure the FIREBASE_PROJECT_ID correct? 401 mean you are not auth – Evyatar Jan 16 '18 at 15:00
  • And You should use the server api key from your firebase console project, at the cloud messaging tab project keys: https://console.firebase.google.com/project/firebase-probiz/settings/cloudmessaging – Evyatar Jan 16 '18 at 15:06
  • Tried it with a newly created Server key and get the same response... – Shai Jan 16 '18 at 15:23
  • Possible duplicate of [Send push to Android by C# using FCM (Firebase Cloud Messaging)](https://stackoverflow.com/questions/37412963/send-push-to-android-by-c-sharp-using-fcm-firebase-cloud-messaging) – Liam Jun 27 '18 at 10:58

1 Answers1

0
wr.Headers.Add("Authorization", "Key='" + FIREBASE_PROJECT_ID +"'");

Try removing the single quote:

wr.Headers.Add("Authorization", "Key=" + FIREBASE_PROJECT_ID);
Verendus
  • 1,026
  • 1
  • 13
  • 26