Setting up WNS service for windows phone 8 get error after add <Identity> tag -
i setting windows phone 8.1 push notification urbanairship. have sid , secret key app. when following step mentioned in dev.windows wns-->live service site:
to set application's identity values manually, open appmanifest.xml file in text editor , set these attributes of element using values shown here.
my application stop working. 1 provide me steps set wns in windows phone 8.1 helps me lot, spend on week on , frustrating.
i got success push notification in windows phone 8.1/ windows apps (universal apps). have implemented sending push notification devices our own web service.
step 1: client secret , package sid dev.windows.com >> services >> live services. you'll need these later in web service.
step 2: in windows app, have associate app store. that, right click on project >> store >> associate app store. log in dev account , associate app.
step 3 you'll need channel uri. in mainpage.xaml, add button , textblock channel uri. xaml code looks this:
<page x:class="finalpushnotificationtest.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:finalpushnotificationtest" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" background="{themeresource applicationpagebackgroundthemebrush}"> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="*" /> </grid.rowdefinitions> <textblock text="push notification" margin="20,48,10,0" style="{staticresource headertextblockstyle}" textwrapping="wrap" /> <scrollviewer grid.row="1" margin="20,10,10,0"> <stackpanel x:name="resultspanel"> <button x:name="pushchannelbtn" content="get channel uri" click="pushchannelbtn_click" /> <progressbar x:name="channelprogress" isindeterminate="false" visibility="collapsed" /> <textblock x:name="channeltext" fontsize="22" /> </stackpanel> </scrollviewer> </grid>
step 4: in mainpage.xaml.cs page, add following code snippet i.e. button click event. when run app, you'll channel uri in console window. note down channel uri, you'll need in web service.
private async void pushchannelbtn_click(object sender, routedeventargs e) { var channel = await pushnotificationchannelmanager.createpushnotificationchannelforapplicationasync(); channeltext.text = channel.uri.tostring(); debug.writeline(channel.uri); }
step 5: need web service send push notification device. right click on project in solution explorer. add >> new project >> visual c# >> web >> asp.net web application. click ok, on template select empty. after add new web form in web application. name sendtoast.
step 6: in sendtoast.aspx.cs, need implement methods , functions access token using package sid, client secret , channel uri. add package sid, cleint secret, , channel uriin respective places. complete code looks following code snippet:
using microsoft.servicebus.notifications; using system; using system.collections.generic; using system.diagnostics; using system.io; using system.linq; using system.net; using system.runtime.serialization; using system.runtime.serialization.json; using system.text; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace sendtoast { public partial class sendtoast : system.web.ui.page { private string sid = "your package sid"; private string secret = "your client secret"; private string accesstoken = ""; [datacontract] public class oauthtoken { [datamember(name = "access_token")] public string accesstoken { get; set; } [datamember(name = "token_type")] public string tokentype { get; set; } } oauthtoken getoauthtokenfromjson(string jsonstring) { using (var ms = new memorystream(encoding.unicode.getbytes(jsonstring))) { var ser = new datacontractjsonserializer(typeof(oauthtoken)); var oauthtoken = (oauthtoken)ser.readobject(ms); return oauthtoken; } } public void getaccesstoken() { var urlencodedsid = httputility.urlencode(string.format("{0}", this.sid)); var urlencodedsecret = httputility.urlencode(this.secret); var body = string.format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=notify.windows.com", urlencodedsid, urlencodedsecret); var client = new webclient(); client.headers.add("content-type", "application/x-www-form-urlencoded"); string response = client.uploadstring("https://login.live.com/accesstoken.srf", body); var oauthtoken = getoauthtokenfromjson(response); this.accesstoken = oauthtoken.accesstoken; } protected string posttocloud(string uri, string xml, string type = "wns/toast") { try { if (accesstoken == "") { getaccesstoken(); } byte[] contentinbytes = encoding.utf8.getbytes(xml); webrequest webrequest = httpwebrequest.create(uri); httpwebrequest request = webrequest httpwebrequest; webrequest.method = "post"; webrequest.headers.add("x-wns-type", type); webrequest.headers.add("authorization", string.format("bearer {0}", accesstoken)); stream requeststream = webrequest.getrequeststream(); requeststream.write(contentinbytes, 0, contentinbytes.length); requeststream.close(); httpwebresponse webresponse = (httpwebresponse)webrequest.getresponse(); return webresponse.statuscode.tostring(); } catch (webexception webexception) { string exceptiondetails = webexception.response.headers["www-authenticate"]; if ((exceptiondetails != null) && exceptiondetails.contains("token expired")) { getaccesstoken(); return posttocloud(uri, xml, type); } else { return "exception: " + webexception.message; } } catch (exception ex) { return "exception: " + ex.message; } } protected void page_load(object sender, eventargs e) { string channeluri = "your channel uri"; if (application["channeluri"] != null) { application["channeluri"] = channeluri; } else { application.add("channeluri", channeluri); } if (application["channeluri"] != null) { string astrreq = application["channeluri"] string; string toast1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?> "; string toast2 = @"<toast> <visual> <binding template=""toasttext01""> <text id=""1"">hello push notification!!</text> </binding> </visual> </toast>"; string xml = toast1 + toast2; response.write("result: " + posttocloud(astrreq, xml)); } else { response.write("application 'channeluri=' has not been set yet"); } response.end(); } } }
run web application, you'll result ok response if sends push notification.
let me know if need working sample project. hope helps. thanks!
Comments
Post a Comment