Beispiel #1
0
        protected override async Task OnInitializedAsync()
        {
            StateTchatContainer.OnChange += StateHasChanged;
            var authState = await authenticationStateTask;

            _user = authState.User;

            var accessTokenResult = await tokenProvider.RequestAccessToken();

            if (!accessTokenResult.TryGetToken(out var accessToken) && _user.Identity.IsAuthenticated)
            {
                navigationManager.NavigateTo("/Identity/Account/Login", true);
            }

            if (_user.Identity.IsAuthenticated)
            {
                StateTchatContainer.MyContacts.Clear();
                Console.WriteLine($"start get notification for user {_user.Identity.Name}");

                var getMyContacts = await httpClient.GetAsync("api/Contacts");

                getMyContacts.EnsureSuccessStatusCode();

                var jsonContact = await getMyContacts.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(jsonContact))
                {
                    StateTchatContainer.SetMyContacts(JsonConvert.DeserializeObject <List <ViewContact> >(jsonContact));
                }

                var getResponse = await httpClient.GetAsync("api/Tchat/GetMessageUnread");

                getResponse.EnsureSuccessStatusCode();

                var jsonResp = await getResponse.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(jsonResp))
                {
                    var listContactUnread = JsonConvert.DeserializeObject <List <string> >(jsonResp);
                    foreach (var item in StateTchatContainer.MyContacts)
                    {
                        if (listContactUnread.Contains(item.UserId.ToString()))
                        {
                            item.HasNewMsg = true;
                        }
                    }
                }

                var getNotifications = await httpClient.GetAsync("api/Notification");

                getNotifications.EnsureSuccessStatusCode();

                NotificationClient = new NotificationClient(navigationManager.ToAbsoluteUri("/notification").AbsoluteUri, accessToken.Value);

                await NotificationClient.StartAsync();

                NotificationClient.NotificationEvent    += ShowNotification;
                NotificationClient.MessageReceivedEvent += MessageReceived;

                var json = await getNotifications.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(json))
                {
                    var temp = JsonConvert.DeserializeObject <List <Notification> >(json);
                    await NotificationClient.SendNotifications(temp);
                }
            }
        }