Пример #1
0
        public void SendNotifications(IEnumerable <Notification> notifications)
        {
            var task = notificationService.DeleteOlderThanAsync(DateTime.Now - RetryPeriod);

            task.Wait();

            var retryList = notificationService.GetAll(Type).ToList();
            var tasks     = new List <Task>();

            foreach (var notification in retryList)
            {
                if (SendNotification(notification))
                {
                    tasks.Add(notificationService.DeleteAsync(notification));
                }
            }

            Task.WaitAll(tasks.ToArray());
            tasks.Clear();

            foreach (var notification in notifications)
            {
                if (!SendNotification(notification))
                {
                    tasks.Add(notificationService.AddAsync(notification, Type));
                }
            }

            Task.WaitAll(tasks.ToArray());
        }
        public async Task <ActionResult> PostAsync([FromBody] PushNotificationModel model, [FromQuery] string lang)
        {
            if (ModelState.IsValid)
            {
                var restrictions = await RestrictionsByNotification(lang);

                if (!restrictions.Restrictions.Any())
                {
                    const string langRus       = "Рус";
                    var          language      = lang == langRus ? "ru" : "en";
                    var          topicLanguage = model.IsSendToAllDevices ? "all" : $"lang_{language}";
                    var          clickAction   = $"message_{language}";

                    var androidNotifyResult = await _notificationService.PushNotificationAsync(
                        JsonConvert.SerializeObject(model.ToAndroidNotification(topicLanguage, clickAction)));

                    var iosNorifyResult = await _notificationService.PushNotificationAsync(
                        JsonConvert.SerializeObject(model.ToIosNotification(topicLanguage, clickAction),
                                                    Formatting.None,
                                                    new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    }));

                    if (!string.IsNullOrEmpty(androidNotifyResult) || !string.IsNullOrEmpty(iosNorifyResult))
                    {
                        var notification = await _notificationService.AddAsync(model.ToNotification(lang));

                        var isSuccess = !string.IsNullOrEmpty(notification.Id);

                        return(isSuccess ? (ActionResult)Ok(notification) : BadRequest("Notification not saved"));
                    }
                    return(BadRequest("Sorry, but something went wrong"));
                }
                return(BadRequest(restrictions));
            }
            return(BadRequest(ModelState));
        }