djfoxer.dp.notification.Core.Logic.DpLogic.GetNotifications C# (CSharp) Method

GetNotifications() public method

public GetNotifications ( ) : Task>
return Task>
        public async Task<List<Notification>> GetNotifications()
        {
            HttpResponseMessage response = null;
            HttpRequestMessage request = null;

            using (var httpClient = new HttpClient())
            {
                request = new HttpRequestMessage(HttpMethod.Get, new Uri(Const.UrlNotifyWithTimeStamp));
                response = await httpClient.SendRequestAsync(request);
            }

            var respList = (JObject)JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());

            List<Notification> notList = new List<Notification>();

            if (respList.HasValues)
            {
                var c = respList.First.First;

                for (int i = 0; i < c.Count(); i++)
                {
                    var ele = (JProperty)c.ElementAt(i);
                    Notification n = JsonConvert.DeserializeObject<Notification>(ele.Value.ToString());

                    n.AddedDate = new DateTime(1970, 1, 1).AddMilliseconds((long)(((JValue)ele.Value["Data"]).Value));
                    n.PublicationId = ele.Name.Split(':')[0];
                    n.Id = ele.Name.Split(':')[1];
                    n.TypeValue = Enum.ParseToNotificationType(((JValue)ele.Value["Type"]).Value.ToString());
                    notList.Add(n);
                }
            }
            notList = notList.OrderBy(n => n.Status).ThenByDescending(n => n.AddedDate).ToList();

            return notList;
        }

Usage Example

        public async void Run(IBackgroundTaskInstance taskInstance)
        {

            BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
            try
            {
                IStorageService storage = new StorageService();
                if (storage.LoadLastUser())
                {
                    DpLogic dpLogic = new DpLogic();
                    ToastLogic toastLogic = new ToastLogic();

                    var notifications = await dpLogic.GetNotifications();

                    //toastLogic.ShowToast(notifications.First(), false);

                    notifications = storage.SaveNotifications(notifications);

                    notifications.ForEach(x => toastLogic.ShowToast(x, false));


                }
            }
            catch (Exception)
            {

            }
            finally
            {
                _deferral.Complete();
            }
        }