javascript - WinJS app with C# background task for push notifications -
i'm trying create background task push notifications in c# linked winjs app (before asking: reason not in js because bug in windows phone runtime, see here).
here 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 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 questions remaining:
how integrate winjs app? adding reference , adding appxmanifest? or need register task manually accessing like
var task = new libraryname.mypushnotificationbgtask();
can access same localsettings backgroundtask when defined in appxmanifest? clicking on raised notification cause opening the(correct) app?
thanks in advance!
typically you'd having c# background task project part of same vs solution, , add reference js app project background task project.
i recommend not ever instantiating c# component/class js app, not necessary background task, , should avoid having pull in weight of clr , associated cost performance/memory/etc.
in manifest editor, you'll want add declaration background task, , specify "entry point" "libraryname.mypushnotificationbgtask". don't specify executable or start page fields.
in js code (probably sometime after app startup), you'll want register task. example:
var bg = windows.applicationmodel.background; var taskbuilder = new bg.backgroundtaskbuilder(); var trigger = new bg.pushnotificationtrigger(); taskbuilder.settrigger(trigger); // must match class name , registration in manifest taskbuilder.taskentrypoint = "libraryname.mypushnotificationbgtask"; taskbuilder.name = "pushnotificationbgtask"; taskbuilder.register(); there apis enumerating tasks (so can see if you've registered it/them), clearing them, etc. there few different techniques avoid registering them multiple times, i'm guessing can figure part out :-)
Comments
Post a Comment