//public async Task<int> UpdateNotification(AndroidToken notificationToken) //{ // var query = @"UPDATE [dbo].[AndroidToken] // SET[UpdatedAt] = @UpdatedAt // ,[CompanyId] = @CompanyId // ,[UserId] = @UserId // WHERE [Id] = @Id"; // notificationToken.UpdatedAt = DateTime.UtcNow; // using (IDbConnection conn = Connection) // { // conn.Open(); // return await conn.ExecuteAsync(query.ToString(), // new // { // notificationToken.UpdatedAt, // notificationToken.CompanyId, // notificationToken.UserId, // notificationToken.Id // }); // } //} public async Task <int> UpdateNotification(AndroidToken notificationToken) { notificationToken.UpdatedAt = DateTime.UtcNow; var query = @"UPDATE [dbo].[AndroidToken] SET [UpdatedAt] = @UpdatedAt ,[DeletedAt] = @DeletedAt WHERE [Id] = @Id AND [CompanyId] = @CompanyId AND [UserId] = @UserId AND [ApplicationId] = @ApplicationId"; using (IDbConnection conn = Connection) { conn.Open(); return(await conn.ExecuteAsync(query.ToString(), new { notificationToken.UpdatedAt, notificationToken.DeletedAt, notificationToken.Id, notificationToken.CompanyId, notificationToken.UserId, notificationToken.ApplicationId })); } }
public async Task <int> CreateNotification(AndroidToken notificationToken) { var query = @"INSERT Into [dbo].[AndroidToken] ([Id], [CreatedAt], [CompanyId], [UserId], [ApplicationId]) Values(@Id, @CreatedAt, @CompanyId, @UserId, @ApplicationId)"; notificationToken.CreatedAt = DateTime.UtcNow; using (IDbConnection conn = Connection) { conn.Open(); return(await conn.ExecuteAsync(query.ToString(), new { notificationToken.Id, notificationToken.CreatedAt, notificationToken.CompanyId, notificationToken.UserId, notificationToken.ApplicationId })); } }
private async Task SendPushAndroidAsync(AndroidNotification androidNotification, Notification notification, AndroidToken androidToken, object data, AndroidTokenRepository androidTokenRepository) { dynamic model = new ExpandoObject(); if (data != null) { model = data; model.UserId = androidToken.UserId; model.CompanyId = androidToken.CompanyId; } //Analisar necessidade de setar a messagem em mais atributos //string alert; string body = notification.Message; string title = notification.Title; string to = androidNotification.TokenId; //int badge = 1; //string sound = "default"; //string vibrate = "true"; // Get the server key from FCM console var serverKey = string.Format("key={0}", Config.GetEnvironmentVariable("Firebase_ServerKey")); // Get the sender id from FCM console var senderId = string.Format("id={0}", Config.GetEnvironmentVariable("Firebase_SenderId")); object payLoad; if (data is null) { payLoad = new { to, notification = new { title, body } }; } else { payLoad = new { to, notification = new { title, body }, data = new { model } }; } var client = new RestClient("https://fcm.googleapis.com/fcm/send"); var request = new RestSharp.RestRequest(Method.POST); IRestResponse response; request.JsonSerializer = new NewtonsoftJsonSerializer(); request.RequestFormat = DataFormat.Json; request.AddHeader("Authorization", serverKey); request.AddHeader("Sender", senderId); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(payLoad); response = client.Execute(request); try { FcmResponse resposta = new FcmResponse(); resposta = JsonConvert.DeserializeObject <FcmResponse>(response.Content); if (resposta.Results != null && resposta.Results.Count() > 0) { androidNotification.Message_Id = resposta.Results.FirstOrDefault().Message_id; } androidNotification.Multicast_Id = resposta.Multicast_id; androidNotification.Success = resposta.Success; if (androidNotification.Success == 0) { try { androidToken.DeletedAt = DateTime.UtcNow; await androidTokenRepository.UpdateNotification(androidToken); } catch { } } } catch (Exception) { androidToken.DeletedAt = DateTime.UtcNow; await androidTokenRepository.UpdateNotification(androidToken); } }