public async Task UpdateTokenAsync()

in src/Covi/Features/PushNotifications/Services/PushNotificationsService.cs [44:87]


        public async Task UpdateTokenAsync()
        {
            await Task.Run(async () =>
            {
                try
                {
                    var isInitialized = await InitializeAsync();
                    if (!isInitialized)
                    {
                        return;
                    }

                    var pushNotificationToken =
                        await PushNotificationsContainer.Instance.GetAsync().ConfigureAwait(false);

                    // Delay initialization to give time for the token to be generated if it is the first run
                    if (string.IsNullOrWhiteSpace(pushNotificationToken?.PushNotificationToken) && _isFirstRun)
                    {
                        await Task.Delay(5000).ConfigureAwait(false);
                        pushNotificationToken = await PushNotificationsContainer.Instance.GetAsync().ConfigureAwait(false);
                    }

                    _isFirstRun = false;

                    if (pushNotificationToken == null)
                    {
                        return;
                    }

                    var currentToken = pushNotificationToken.PushNotificationToken;

                    //Send token only once per session or if changed
                    if (!string.Equals(_lastSentToken, currentToken, StringComparison.OrdinalIgnoreCase))
                    {
                        var profileResponse = await _platformClient.Endpoints.SetupNotificationsWithHttpMessagesAsync(pushNotificationToken).ConfigureAwait(false);
                        _lastSentToken = currentToken;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Failed to update push notification token for the device");
                }
            }).ConfigureAwait(false);
        }