Needless to say but they should really only be used to fire small bits of code to inform your user of something and not used for any heavy lifting.
To get started you're going to have to add a WinMD project to your solution.
with that done add a reference from your Application Project to the WinMD project.
in the above the Application project is pc.Background and the WinMD project is pc.BackgorundTask, with your reference made open up the only class in your WinMD Project, it should look pretty bare bones.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace pc.BackgroundTask
{
public sealed class Class1
{
}
}
with our changes complete we should have something along the lines of
using Windows.ApplicationModel.Background;
namespace pc.BackgroundTask
{
public sealed class AppendedToLockScreenBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
//Our
Logic goes here
deferral.Complete();
}
}
}
With our Background Task, written we now need to go back to our application project and add some declarations into our manifest, so open up the "Package.appxmanifest" file and go to the declarations tab. Once there add the Background Tasks declaration specify the System Event and set the Entry Point to the Namespace.Class that implements your Run method
Now that's done, go back to the Application Tab and Set the Lock screen notification to Badge
and set the Toast Capable to Yes
Next you'll have to add the visual assets for badge notifications, i just made min in MS Paint,
Now with all that configuration out of the way let's register our background Task, open up your MainPage.xaml View and in the constructor register the event
//check
to make sure that your background task isn't already registred
if (BackgroundTaskRegistration.AllTasks.FirstOrDefault(t => t.Value.Name == "AppendToLockScreen").Value == null)
{
var builder = new BackgroundTaskBuilder();
builder.Name = "AppendToLockScreen";
builder.TaskEntryPoint = "pc.BackgroundTask.AppendedToLockScreenBackgroundTask";
builder.SetTrigger(new SystemTrigger(SystemTriggerType.LockScreenApplicationAdded,
false));
var taskRegistration = builder.Register();
}