0

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"}

1 respuesta 1

0

Este codigo funciono perfecto:

   public async Task<bool> PushServer(string token, string endPoint = "https://fcm.googleapis.com/fcm/send")
        {
            bool result = false;
            string jsonData = SetContentPush(token);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, endPoint);
            request.Headers.TryAddWithoutValidation("Authorization", "key=" + EngineData.KeyServer); 
            request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json"); 
            HttpResponseMessage resultado = new HttpResponseMessage(); 
            using (HttpClient client = new HttpClient()) 
            { 
                resultado = await client.SendAsync(request);
                result = true;
            }
            return result;
        }

        private string  SetContentPush(string token)
        {
            FirebasePush model = new FirebasePush()
            {
                notification = new FirebasePush.Notification() 
                {
                    title = "Social Wifi",
                    body = "Tienes una nueva notificacion 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
            };
            string resultado = JsonConvert.SerializeObject(model);
            return resultado;
        }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.