Пример #1
0
        public async void EnviarNotificacion(Models.Usuarios_Disp[] dispositivos, decimal viajeId, string titulo, string mensaje, Accion accion)
        {
            // Inicializo los objetos para hacer el Request
            var client  = new HttpClient();
            var request = new HttpRequestMessage();

            request.Method = HttpMethod.Post;

            // Arma la URL para enviar la notificación
            request.RequestUri = new Uri("https://fcm.googleapis.com/fcm/send");
            request.Headers.TryAddWithoutValidation("Authorization", "key=" + Startup.Firebase.CloudMessagingKey);

            // Arma la lista de tokens
            var tokens = new List <string>();

            foreach (var d in dispositivos)
            {
                tokens.Add(d.DispositivoId);
            }

            // Crea la instancia de la notificación
            Notificacion notificacion = new Notificacion(tokens.ToArray(), viajeId, titulo, mensaje, accion);

            // Adjunta el body
            var json = JsonConvert.SerializeObject(notificacion);

            Consola.Debug("json", json);

            request.Content = new StringContent(json);

            // Adjunta los headers
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            // Envía el request al Firebase
            var response = await client.SendAsync(request);

            // Crea los datos del registro a insertar
            var r = new Models.Notificaciones
            {
                NotFchHr     = DateTime.Now,
                ViajeId      = viajeId,
                NotMensaje   = titulo + " - " + mensaje + " - Data: " + json,
                NotRespuesta = response.Content.ReadAsStringAsync().Result,
                NotEst       = response.StatusCode.ToString()
            };

            // Uso una nueva conexión porque este método se llama asíncronamente
            using (var db2 = new VolquexDB())
                // Creo un registro con cada token
                foreach (var disp in dispositivos)
                {
                    r.UsuarioId     = disp.UsuarioId;
                    r.DispositivoId = disp.DispositivoId;

                    // Hago el insert
                    db2.InsertWithDecimalIdentity(r);
                }

            client.Dispose();
        }
Пример #2
0
        public static int Grabar(string Titulo, string Desc, string FechaEnvio)
        {
            try
            {
                Models.Notificaciones objNotif = new Models.Notificaciones()
                {
                    Titulo      = Titulo,
                    Descripcion = Desc,
                    Fecha       = Convert.ToDateTime(FechaEnvio),
                    Prioridad   = 1,
                    Tipo        = "S",
                    Icono       = ""
                };

                ApiServices         objApi   = new ApiServices();
                HttpResponseMessage response = null;
                string Request = Newtonsoft.Json.JsonConvert.SerializeObject(objNotif);
                response = objApi.CallService("Notificaciones", Request, ApiServices.TypeMethods.POST).Result;

                if (response.IsSuccessStatusCode)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }
            catch (Exception ex)
            {
                //Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                return(0);
            }
        }
        public void NotificaError(string message)
        {
            _logger.LogError(message);
            string        json     = "";
            IRestResponse response = null;

            Models.Notificaciones model = new Models.Notificaciones();

            model.id_tipo_notificacion = Convert.ToInt64(_envVariables.tipoNotificacion).ToString();
            model.descripcion          = message;
            model.id_carga             = _envVariables.ControlCarga.idCarga.ToString();
            model.id_descarga          = _envVariables.ControlDescarga.idDes;
            model.parametros           = new List <parametros>();
            model.parametros.Add(new parametros()
            {
                idAI = _envVariables.ArchivoInstancia.id, servicio = "Carga - ProcesaArchivo"
            });
            json = JsonConvert.SerializeObject(model);
            _logger.LogInformation("json: {0}", json);
            try
            {
                var client  = new RestClient(_envVariables.UrlNotificaciones);
                var request = new RestRequest(Method.POST);
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("undefined", json, ParameterType.RequestBody);
                response = client.Execute(request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
            }
            finally
            {
                if (Convert.ToInt16(response.StatusCode) != 200)
                {
                    _logger.LogError("Respuesta servicio Content:{0}", response.Content);
                    _logger.LogError("Respuesta servicio Exception:{0}", response.ErrorException);
                    _logger.LogError("Respuesta servicio ErrorMessage:{0}", response.ErrorMessage);
                }
            }
        }
Пример #4
0
 public Models.Notificaciones Actualizar(Models.Notificaciones o)
 {
     db.Update(o);
     return(o);
 }
Пример #5
0
 public decimal Insertar(Models.Notificaciones o)
 {
     return(db.InsertWithDecimalIdentity(o));
 }