private Task GetSendNotificationTask(IntegratedAppUserSetting setting, SendUserNotificationViaApplication model) { return(setting.App.AppType switch { IntegratedAppType.IosApplication => GetSendNotificationTaskForIosApp(setting, model), IntegratedAppType.AndroidApplication => Task.CompletedTask, _ => throw new ArgumentOutOfRangeException(), });
/// <summary> /// Отправить уведомление для Ios приложения /// </summary> /// <param name="setting"></param> /// <param name="reqModel"></param> /// <returns></returns> public async Task GetSendNotificationTaskForIosApp(IntegratedAppUserSetting setting, SendUserNotificationViaApplication reqModel) { var model = new SendNotificationViaOneSignal { AppId = setting.App.Uid, Data = new Dictionary <string, string> { ["foo"] = "bar" }, Contents = new Dictionary <string, string> { ["en"] = "English Message", ["ru"] = reqModel.Text }, Headings = new Dictionary <string, string> { ["en"] = "111", ["ru"] = reqModel.Title }, IncludePlayerIds = new[] { setting.UserUidInApp } }; var request = new HttpRequestMessage(HttpMethod.Post, "https://onesignal.com/api/v1/notifications") { Content = CreateRequestStringContent(model) }; request.Headers.Authorization = new AuthenticationHeaderValue("Authorization", $"Basic {setting.App.ConfigurationJson}"); try { var response = await HttpClient.SendAsync(request); } catch (Exception error) { Logger.LogError(error, "IntegratedAppWorker.GetSendNotificationTaskForIosApp.OnException"); } }
/// <summary> /// Отправить уведомления небезопасно /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task <BaseApiResponse> SendUserNotificationViaIntegratedApplicationUnsafeAsync(SendUserNotificationViaApplication model) { var settings = await Query <IntegratedAppUserSetting>() .Include(x => x.App) .Where(x => x.Active && x.UserId == model.UserId) .ToListAsync(); if (settings.Count == 0) { return(new BaseApiResponse(true, "Уведомления не были отправлены, так как не было найдено подходящих настроек с интегрированными приложениями")); } var tasks = settings.Select(x => GetSendNotificationTask(x, model)); await Task.WhenAll(tasks); return(new BaseApiResponse(true, "Уведомления были отправлены")); }