public async Task <bool> SendNotificationAsync(DTO_NotificationForm form) { var serverKey = "key=" + _configuration["FirebaseConfig:serverKey"]; var senderId = "id=" + _configuration["FirebaseConfig:senderId"]; var data = new { //To = form., notification = new { Title = form.Title, Body = form.Body } }; var jsonBody = JsonConvert.SerializeObject(data, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send")) { httpRequest.Headers.TryAddWithoutValidation("Authorization", serverKey); httpRequest.Headers.TryAddWithoutValidation("Sender", senderId); httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json"); using (var httpClient = new HttpClient()) { var result = await httpClient.SendAsync(httpRequest); return(result.IsSuccessStatusCode); } } }
public async Task <IActionResult> SendNotificatin([FromBody] DTO_NotificationForm form) { try { await _firebaseService.SendNotificationToTopic(form); return(Ok()); } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task SendNotificationToTopic(DTO_NotificationForm form) { var topic = PrepareTopic(form.CompanyId, form.PlatoonId, form.OnlyAssistants); var message = new Message() { Notification = new Notification() { Title = form.Title, Body = form.Body }, Topic = topic, }; string response = await FirebaseMessaging.DefaultInstance.SendAsync(message); }