c# - How to send and receive push notifications for Windows Phone 8.1 -
i followed microsofts article sending , receiving push notifications on windows phone 8.0:
https://msdn.microsoft.com/en-us/library/windows/apps/hh202967(v=vs.105).aspx
it works fine, creating new windows phone 8.1 app , try rewrite same 8.0 code, classes not available in wp 8.1.
please me how can implement these windows phone 8.1.
here class use receiving push notifications , handling channeluri
. call updatechanneluri
method. channeluri updated if need , channeluriupdated
event fired , same saved application data settings.
if app running , receive notification, 1 of 4 methods notification content executed, determined notification type.
public sealed class pushservice { private const string channelurikey = "channeluri"; private const string channeluridefault = null; private pushnotificationchannel _channel; private string _channeluri; /// <summary> /// initializes new instance of <see cref="services.pushservice"/> class. /// </summary> public pushservice() { this._channeluri = localsettingsload(applicationdata.current.localsettings, channelurikey, channeluridefault); } /// <summary> /// gets push notification channel uri. if no channel uri yet created /// value <c>null</c>. /// </summary> public string channeluri { { return _channeluri; } private set { if (_channeluri != value) { this._channeluri = value; localsettingsstore(applicationdata.current.localsettings, channelurikey, value); } } } /// <summary> /// requests new push channel uri. /// </summary> public async task<string> updatechanneluri() { var retries = 3; var difference = 10; // in seconds var currentretry = 0; { try { _channel = await pushnotificationchannelmanager.createpushnotificationchannelforapplicationasync(); _channel.pushnotificationreceived += onpushnotificationreceived; if (!_channel.uri.equals(channeluri)) { channeluri = _channel.uri; // todo send channel uri server server this.raisechanneluriupdated(); return _channel.uri; } } catch { // not create channel } await task.delay(timespan.fromseconds(difference)); } while (currentretry++ < retries); return null; } private void onpushnotificationreceived(pushnotificationchannel sender, pushnotificationreceivedeventargs args) { switch (args.notificationtype) { case pushnotificationtype.badge: this.onbadgenotificationreceived(args.badgenotification.content.getxml()); break; case pushnotificationtype.tile: this.ontilenotificationreceived(args.tilenotification.content.getxml()); break; case pushnotificationtype.toast: this.ontoastnotificationreceived(args.toastnotification.content.getxml()); break; case pushnotificationtype.raw: this.onrawnotificationreceived(args.rawnotification.content); break; } args.cancel = true; } private void onbadgenotificationreceived(string notificationcontent) { // code when badge notification received when app running } private void ontilenotificationreceived(string notificationcontent) { // code when tile notification received when app running } private void ontoastnotificationreceived(string notificationcontent) { // code when toast notification received when app running // show toast notification programatically var xmldocument = new xmldocument(); xmldocument.loadxml(notificationcontent); var toastnotification = new toastnotification(xmldocument); //toastnotification.suppresspopup = true; toastnotificationmanager.createtoastnotifier().show(toastnotification); } private void onrawnotificationreceived(string notificationcontent) { // code when raw notification received when app running } public event eventhandler<eventargs> channeluriupdated; private void raisechanneluriupdated() { if (channeluriupdated != null) { channeluriupdated(this, new eventargs()); } } public static t localsettingsload<t>(applicationdatacontainer settings, string key, t defaultvalue) { t value; if (settings.values.containskey(key)) { value = (t)settings.values[key]; } else { // otherwise use default value. value = defaultvalue; } return value; } public static bool localsettingsstore(applicationdatacontainer settings, string key, object value) { bool valuechanged = false; if (settings.values.containskey(key)) { // if key exists if (settings.values[key] != value) { // if value has changed, store new value settings.values[key] = value; valuechanged = true; } } else { // otherwise create key settings.values.add(key, value); valuechanged = true; } return valuechanged; } }
Comments
Post a Comment