Ejemplo n.º 1
0
        private async void ChangePushState(bool isOn)
        {
            IsPushStateChanging = true;

            byte currentRetry = 0;

            while (currentRetry < 4)
            {
                try
                {
                    if (isOn)
                    {
                        await pushNotificationsService.RegisterDevice();
                    }
                    else
                    {
                        await pushNotificationsService.UnregisterDevice();
                    }

                    settingsService.Set(ENABLE_PUSH_NOTIFICATIONS, isOn);
                    break;
                }
                catch (Exception ex)
                {
                    if (++currentRetry < 4)
                    {
                        int timeout      = currentRetry * 5;
                        var notification = new AppNotification
                        {
                            Type     = AppNotificationType.Warning,
                            Title    = ex.Message,
                            Content  = $"Повтор через {timeout} секунд",
                            Duration = TimeSpan.FromSeconds(timeout)
                        };
                        appNotificationsService.SendNotification(notification);
                        await Task.Delay(timeout * 1000);
                    }
                    else
                    {
                        settingsService.Set(ENABLE_PUSH_NOTIFICATIONS, !isOn);
                        var notification = new AppNotification
                        {
                            Type    = AppNotificationType.Error,
                            Title   = $"Не удалось {(isOn ? "включить" : "отключить")} Push-уведомления",
                            Content = "Возвращены предыдущие настройки"
                        };
                        appNotificationsService.SendNotification(notification);
                    }
                }
            }

            IsPushStateChanging = false;
        }