I've followed the Microsoft guidelines for creating and registering a background task: https://msdn.microsoft.com/en-us/library/windows/apps/mt299100.aspx
I have my main project, which registers the background task with a trigger etc, and another Windows Runtime Component project which holds the code for the background task, and is referenced by my main project.
My code to register the background task is:
bool registered = false;
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == "BackgroundGPS")
{
registered = true;
}
}
if (!registered)
{
var builder = new BackgroundTaskBuilder();
builder.Name = "BackgroundGPS";
builder.TaskEntryPoint = "BackgroundTasks.BackgroundGPSTask";
BackgroundExecutionManager.RemoveAccess();
var x = await BackgroundExecutionManager.RequestAccessAsync();
var trigger = new TimeTrigger(15, false);
builder.SetTrigger(trigger);
BackgroundTaskRegistration task = builder.Register();
//Here trigger property of 'task' is null.
}
}
My background task is never run because it appears the trigger never fires. I've debugged the code, and as per my comment, the trigger property of the BackgroundTaskRegistration variable is null.
Now, I've tried using a SystemTrigger instead of a TimeTrigger and it works, the property is not null. I see that the trigger property is described as 'being null if the trigger type is unsupported'.
Why would TimeTrigger be unsupported? I've called RequestAccessAsync()
to get lock-screen access, and it comes back as allowed. I've also added 'Timer' to the task types of the 'Background Task' declaration of my main project's app manifest. By all suggestions from Microsoft the TimeTrigger is the only way to get a background task to run on a timer (MaintenanceTrigger only works when plugged into power).
There are two similar SO questions about this:
Can't register a Time triggered Background Task
BackgroundTaskRegistration trigger property is null
One of which doesn't have any answers on, and the other's suggestions I've already implemented.
Is anyone able to help?