Estoy desarrollando un método para hacer post desde el servidor a la Api de firebase , escribí esto pero me devuelve el siguiente mensaje:
StatusCode: 401, ReasonPhrase: 'The request was missing an Authentication Key (FCM Token). Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent
public async Task<bool> PushServer(string token, string uid, string endPoint = "https://fcm.googleapis.com/fcm/send")
{
bool result = false;
string respuesta = string.Empty;
string jsonData = SetContentPush(token);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
System.Net.CredentialCache credentialCache = new System.Net.CredentialCache();
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes("key=" + EngineData.KeyServer));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", encoded);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsync(endPoint, new StringContent(jsonData, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
respuesta = await response.Content.ReadAsStringAsync();
result = true;
}
else
{
respuesta = response.IsSuccessStatusCode.ToString();
}
}
return result;
}
Pude hacerlo desde PostMan este es el código:
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Authorization: key= ServerApiKey
Content-Type: application/json
User-Agent: PostmanRuntime/7.20.1
Accept: */*
Cache-Control: no-cache
Postman-Token: dc671f8a-f457-4198-a8f3-436475af542a,0937fc5b-516d-4644-a539-4db5c34d7cd8
Host: fcm.googleapis.com
Accept-Encoding: gzip, deflate
Content-Length: 390
Connection: keep-alive
cache-control: no-cache
{"notificación":{"title":"Social Wifi","body":"Tienes una nueva notificación en Social Wifi","click_action":"https://www.sudokudetodos.com","icon":"http://www.icons101.com/icons/5/Replacement_Icons_by_creatiVe5/128/Telegram.png"},"to":"Token del Usuario Logeado"}