wns - UWP PushNotificationTrigger won't trigger background task -
i trying develop windows 10 uwp app has background task triggered raw pushnotification trigger. test, have handler on app see if raw notification pushing , worked. here code snippets:
pushnotificationchannel _channel = null; private async void appinit() { _channel = await common.wnshelper.getchannelasync(); if (_channel != null) { _channel.pushnotificationreceived += _channel_pushnotificationreceived; } await installpositionbackgroundtask(); } private async void _channel_pushnotificationreceived(pushnotificationchannel sender, pushnotificationreceivedeventargs args) { if (args.notificationtype == pushnotificationtype.raw) { common.generalhelper.sendtoast("at app positionbackgroundtask"); } } private async task installpositionbackgroundtask() { var taskname = "positionbackgroundtask"; foreach (var tsk in backgroundtaskregistration.alltasks) { if (tsk.value.name == taskname) { tsk.value.unregister(true); } } var cost = backgroundworkcost.currentbackgroundworkcost; if (cost == backgroundworkcostvalue.high) { return; } var allowed = await backgroundexecutionmanager.requestaccessasync(); if (allowed == backgroundaccessstatus.allowedsubjecttosystempolicy || allowed == backgroundaccessstatus.alwaysallowed) { var builder = new backgroundtaskbuilder(); builder.name = nameof(positionbackgroundtask.positionbackgroundtask); builder.cancelonconditionloss = false; builder.taskentrypoint = typeof(positionbackgroundtask.positionbackgroundtask).fullname; builder.settrigger(new pushnotificationtrigger()); backgroundtaskregistration task = builder.register(); task.completed += new backgroundtaskcompletedeventhandler(oncompleted); common.generalhelper.sendtoast("position task installed. " + system.datetime.now.tostring()); } } private async void button_click(object sender, routedeventargs e) { // send var response = await common.wnshelper.pushrawasync<common.rawrequest>(_channel.uri, new rawrequest() { requesttype = rawrequesttype.requestposition, fromdeviceid = _alldevices[0].d_id }, null); }
here's background task
namespace positionbackgroundtask { public sealed class positionbackgroundtask { backgroundtaskdeferral _deferral; public async void run(ibackgroundtaskinstance taskinstance) { common.generalhelper.sendtoast("at positionbackgroundtask"); var cancel = new system.threading.cancellationtokensource(); taskinstance.canceled += (s, e) => { cancel.cancel(); cancel.dispose(); }; _deferral = taskinstance.getdeferral(); try { rawnotification notification = (rawnotification)taskinstance.triggerdetails; //: //: //: } { _deferral.complete(); } } } }
here's manifest
<extension category="windows.backgroundtasks" entrypoint="positionbackgroundtask.positionbackgroundtask"> <backgroundtasks> <task type="pushnotification" /> </backgroundtasks> </extension>
here's code send raw notification
public static async task<httpresponsemessage> pushrawasync<t>(string channeluri, rawrequest request, t rawdata) { // app authorization string appdatasecret = myappsecret; uri requesturi = new uri(@"https://login.live.com/accesstoken.srf"); var client = new system.net.http.httpclient(); system.net.http.httpresponsemessage response = await client.postasync(requesturi, new stringcontent(appdatasecret, system.text.encoding.utf8, "application/x-www-form-urlencoded")); string responjsontext = await response.content.readasstringasync(); wmntoken token = jsonconvert.deserializeobject<wmntoken>(responjsontext); string requeststr = jsonconvert.serializeobject(request) + ";" + jsonconvert.serializeobject(rawdata); var content = new streamcontent(new memorystream(encoding.utf8.getbytes(requeststr))); client.defaultrequestheaders.authorization = new system.net.http.headers.authenticationheadervalue(token.token_type, token.access_token); client.defaultrequestheaders.add("x-wns-type", "wns/raw"); response = await client.postasync(channeluri, content); return response; }
the app received notification background task never. did wrong?
more info: looks push notification hitting box got error in event log
activation of app 45737myname.testpushnotificationpartofemail_4rhf8erfmecqa!app windows.backgroundtasks contract failed error: no such interface supported.
and turns out didn't subclass ibackgroundtask should this
public sealed class positionbackgroundtask : ibackgroundtask
background tasks need subclass of ibackgroundtask
public sealed class positionbackgroundtask : ibackgroundtask
Comments
Post a Comment