c# - How to display push notification for user in windows 8.1? -
i working on windows 8.1 push notification part. have read different links , found first need register app , information sid , client secret , send our server team can send push notification.
then after this, implemented following code @ side channeluri , expiration date of uri wns.
pushnotificationchannel channel = null; try { channel = await pushnotificationchannelmanager.createpushnotificationchannelforapplicationasync(); if (channel != null) { var notificationuri = channel.uri; var expiration_time = channel.expirationtime; } channel.pushnotificationreceived += channel_pushnotificationreceived; } catch (exception ex) { if (ex != null) { system.diagnostics.debug.writeline(ex.hresult); } } i have received values , server team added logic send me push notification. now, problem facing not aware how display received push notification sent server user. also, can display notification app not running or in background?
background tasks solved problem.
first need create windowsruntimecomponent project , add code below
public sealed class pushnotification:ibackgroundtask { public void run(ibackgroundtaskinstance taskinstance) { rawnotification notification = (rawnotification)taskinstance.triggerdetails rawnotification; if (notification != null) { toasttemplatetype toasttemplate = toasttemplatetype.toastimageandtext01; xmldocument toastxml = toastnotificationmanager.gettemplatecontent(toasttemplate); var textelemets = toastxml.getelementsbytagname("text"); textelemets[0].appendchild(toastxml.createtextnode(notification.content)); var imageelement = toastxml.getelementsbytagname("image"); imageelement[0].attributes[1].nodevalue = "ms-appx:///assets/50.png"; toastnotificationmanager.createtoastnotifier().show(new toastnotification(toastxml)); } } } then register background task in of page( added in home page) using below code
private async void registerbackgroundtask() { await backgroundexecutionmanager.requestaccessasync(); try { foreach (var task in backgroundtaskregistration.alltasks) { try { task.value.unregister(false); } catch { // } } backgroundtaskbuilder builder = new backgroundtaskbuilder(); builder.name = "push notifcation task"; builder.taskentrypoint = typeof(pushnotification).fullname; builder.settrigger(new pushnotificationtrigger()); builder.register(); } catch(exception e) { if(e != null) { system.diagnostics.debug.writeline(e.hresult); system.diagnostics.debug.writeline(e.innerexception); } } } please don't forget add background task in declarations section in package.appmanifest file , name of entry point should match builder.taskentrypoint = typeof(pushnotification).fullname; else exception.
hope helps someone.
Comments
Post a Comment