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!