/// <summary> /// https://firebase.google.com/docs/cloud-messaging/concept-options#notifications /// </summary> /// <param name="notification"></param> /// <param name="fcmConfig"></param> /// <returns></returns> public async Task <FcmResult> SendAsync(FcmPayload notification, FcmConfig fcmConfig) { if (notification.RegistrationIds?.Count() > 1000) { throw new ArgumentOutOfRangeException($"{nameof(notification.RegistrationIds)} Out Of Range 1000"); } string json = JsonSerializer.Serialize(notification); using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, fcmConfig.FcmUrl)) { httpRequest.Headers.Add("Authorization", $"key = {fcmConfig.ServerKey}"); if (!string.IsNullOrWhiteSpace(fcmConfig.SenderId)) { httpRequest.Headers.Add("Sender", $"id = {fcmConfig.SenderId}"); } httpRequest.Content = new StringContent(json, Encoding.UTF8, "application/json"); HttpClient client = _httpClientFactory.CreateClient("FCM"); using (var response = await client.SendAsync(httpRequest)) { response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); FcmResult result = JsonSerializer.Deserialize <FcmResult>(content); result.FcmPayload = notification; return(result); } } }
public Task <FcmResult> SendAsync(FcmPayload notification) { return(SendAsync(notification, sender => sender.Configs.Single())); }
public Task <FcmResult> SendAsync(FcmPayload notification, Func <IFcmSender, FcmConfig> func) { FcmConfig fcmConfig = func(this); return(SendAsync(notification, fcmConfig)); }