2

I'm trying to create a background task for push notifications in C# that is linked to a winjs app (before anyone asking: reason not to do this in js is because a bug in windows phone runtime, see here).

Here is my first draft:

public sealed class myPushNotificationBgTask : IBackgroundTask {

public void Run(IBackgroundTaskInstance taskInstance)
{
    RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
    string content = notification.Content;

    Debug.WriteLine("received push notification from c# bg task!");

    var settings = ApplicationData.Current.LocalSettings;
    settings.Values["push"] = content;

    raiseToastNotification(content);
}

private void raiseToastNotification(string text)
{
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

    XmlNodeList elements = toastXml.GetElementsByTagName("text");
    foreach (IXmlNode node in elements){
        node.InnerText = text;
    }

    ToastNotification notification = new ToastNotification(toastXml);
    ToastNotificationManager.CreateToastNotifier().Show(notification);
}
}

But there are some questions remaining:

  • How to integrate this to my winjs app? Adding as reference and adding it to my appxmanifest? Or do I need to register the task manually with accessing it like

    var task = new LibraryName.myPushNotificationBgTask();

  • Can I access the same localsettings with this backgroundtask when defined in appxmanifest? Will clicking on the raised notification cause opening the(correct) app?

Thanks in advance!

Community
  • 1
  • 1
kerosene
  • 930
  • 14
  • 31

1 Answers1

3

Typically you'd do this by having the C# background task project as part of the same VS solution, and then add a reference from the JS app project to the background task project.

I would recommend not ever instantiating the C# component/class from your JS app, as this is not necessary for a background task, and it should avoid having to pull in the weight of the CLR and the associated cost to performance/memory/etc.

In the manifest editor, you'll want to add a Declaration for the background task, and specify the "Entry point" as "LibraryName.myPushNotificationBgTask". Don't specify anything for the executable or start page fields.

In your JS code (probably sometime soon after app startup), you'll want to register the task. For example:

var bg = Windows.ApplicationModel.Background;
var taskBuilder = new bg.BackgroundTaskBuilder();
var trigger = new bg.PushNotificationTrigger();
taskBuilder.setTrigger(trigger);
// Must match class name and registration in the manifest
taskBuilder.taskEntryPoint = "LibraryName.myPushNotificationBgTask";
taskBuilder.name = "pushNotificationBgTask";
taskBuilder.register();

There are also APIs for enumerating tasks (so you can see if you've already registered it/them), clearing them, etc. There are a few different techniques to avoid registering them multiple times, but I'm guessing you can figure that part out :-)

Brandon Paddock
  • 312
  • 1
  • 6