SensorbergSDK.BackgroundTaskManager.RegisterBackgroundTaskAsync C# (CSharp) Method

RegisterBackgroundTaskAsync() public method

Register background tasks, by the given configuration.
public RegisterBackgroundTaskAsync ( SdkConfiguration configuration ) : Task
configuration SdkConfiguration Configuration for the new registration.
return Task
        public async Task<BackgroundTaskRegistrationResult> RegisterBackgroundTaskAsync(SdkConfiguration configuration)
        {
            BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult()
            {
                Success = IsBackgroundTaskRegistered,
                Exception = null
            };

            if (!result.Success)
            {
                // Prompt user to accept the request
                BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
                if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity
                    || backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
                {
                    result = RegisterTimedBackgroundTask(configuration.BackgroundTimerClassName);

                    if (result.Success)
                    {
                        result = await RegisterAdvertisementWatcherBackgroundTaskAsync(configuration);
                    }
                }

                if (result.Success)
                {
                    Logger.Debug("BackgroundTaskManager.RegisterBackgroundTask(): Registration successful");
                }
            }
            else
            {
                Logger.Debug("BackgroundTaskManager.RegisterBackgroundTask(): Already registered");
            }

            return result;
        }

Usage Example

 /// <summary>
 /// Registers the background task or in the case of a pending background filter update,
 /// re-registers the task.
 /// </summary>
 /// <returns>The registration result.</returns>
 public async Task <BackgroundTaskRegistrationResult> RegisterBackgroundTaskAsync()
 {
     SdkData.BackgroundTaskEnabled = true;
     return(await _backgroundTaskManager.RegisterBackgroundTaskAsync(Configuration));
 }