public async Task TriggerPush(PushMessage pushMessage) { pushMessage.Message += $"-{DateTime.Now:dddd HH:mm:ss}"; var subscriptions = await _subscriptionsRepository.GetSubscriptionsAsync(); foreach (var sub in subscriptions) { var subscription = new PushSubscription(sub.Endpoint, sub.Key, sub.AuthSecret); var webPushClient = new WebPushClient(); try { webPushClient.SendNotification(subscription, JsonConvert.SerializeObject(pushMessage, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }), _vapidDetails); } catch (WebPushException exception) { Console.WriteLine("Http STATUS code" + exception.StatusCode); } catch (Exception exception) { await _subscriptionsRepository.RemoveSubscription(sub); Console.WriteLine(exception); } } }
public async Task <ActionResult> GetSubscriptions([FromQuery] int userId = 0, string search = null) { var subs = new List <ApiSubscriptions>(); if (userId == 0 && search == null) { subs = (await _repo.GetSubscriptionsAsync()).Select(ApiMapper.MapSub).ToList(); } else { subs = (await _repo.GetSubscriptionsAsync(userId, search)).Select(ApiMapper.MapSub).ToList(); } try { if (subs.Count == 0 && search == null && userId == 0) { return(Ok("There are no bills.")); } else if (subs.Count == 0 && search != null && userId != 0) { return(NotFound($"There are no bills with userId of {userId} and search parameter of '{search}'.")); } else if (subs.Count == 0 && userId != 0) { return(NotFound($"There are no bills with the user Id of {userId}.")); } else if (subs.Count == 0 && search != null) { return(NotFound($"There are bills with '{search}'.")); } else { return(Ok(subs)); } } catch (Exception e) { return(StatusCode(500, e.Message)); } }