Principales respuestas
¿Como crear solicitud POST a Rest API Firebase con C# para enviar notificacion push?

Pregunta
-
Hola...
Nesecito hacer un POST a una Rest API,
la hize con Postman y genero el siguiente codigo:
¿Como puedo hacerlo en C#?POST /fcm/send HTTP/1.1 Host: fcm.googleapis.com Authorization: key= KEY SECRET Content-Type: application/json User-Agent: PostmanRuntime/7.20.1 Accept: */* Cache-Control: no-cache Postman-Token: 00ea82ea-c162-42f7-816c-4098c071bb8e,f582fc3f-2a21-40b4-891b-1dc6b1acbb3c Host: fcm.googleapis.com Accept-Encoding: gzip, deflate Content-Length: 390 Connection: keep-alive cache-control: no-cache {"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 PATNER"}
EFRAIN MEJIAS C VALENCIA - VENEZUELA
- Editado Efrain Mejias Castillo domingo, 24 de noviembre de 2019 8:59
Respuestas
-
El codigo que me retorno Status Code 200..aunque no se muestra el push ...
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 (var 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 = "http://localhost:5000", icon = "http://www.icons101.com/icons/5/Replacement_Icons_by_creatiVe5/128/Telegram.png" }, to = token }; string resultado = JsonConvert.SerializeObject(model); return resultado;
EFRAIN MEJIAS C VALENCIA - VENEZUELA
- Marcado como respuesta Efrain Mejias Castillo domingo, 24 de noviembre de 2019 8:48
Todas las respuestas
-
hola
Tiene que usar la libreria HttpClient
Efficient post calls with HttpClient and JSON.NET
Consume Web API in .NET using HttpClient
defines una clase con la estructura del json que serializas y envias como parte del content de la llamada post
saludos
Leandro Tuttini
Blog
MVP Profile
Buenos Aires
Argentina- Editado Leandro TuttiniMVP sábado, 23 de noviembre de 2019 21:53
- Propuesto como respuesta Pablo RubioModerator lunes, 25 de noviembre de 2019 16:02
-
Hola Leandro...
Pero no explica como se hace con esta linea :
Authorization: key= KEY SECRET
EFRAIN MEJIAS C VALENCIA - VENEZUELA
- Propuesto como respuesta Pablo RubioModerator lunes, 25 de noviembre de 2019 16:02
-
El codigo que me retorno Status Code 200..aunque no se muestra el push ...
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 (var 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 = "http://localhost:5000", icon = "http://www.icons101.com/icons/5/Replacement_Icons_by_creatiVe5/128/Telegram.png" }, to = token }; string resultado = JsonConvert.SerializeObject(model); return resultado;
EFRAIN MEJIAS C VALENCIA - VENEZUELA
- Marcado como respuesta Efrain Mejias Castillo domingo, 24 de noviembre de 2019 8:48