Suppose that a nurse is assisting a patient in some hospital room and that the patient’s doctor is somewhere else in the facility. The nurse gathers vitals for the patient and enters the information into the computer. The nurse would like for the doctor to get this information “on the fly” without having to pull him or her into the room to look at the computer. Solution? Push notifications! What could happen is that when the vitals are entered, the doctor receives a push notification from some cloud service, wherever he or she may be. Thus, this is an example where cloud services enhance efficiency. Pretty cool, huh?
Now, the fun part. How does a developer write code to enable push notifications? Well, this process requires some work, at least for the first time setup, which I’ll cover in detail. In this blog, I will mainly focus on the setup required for push notifications. For brevity, the server backend code for the final step of actually sending push notifications to client(s) is left out and is assumed to already exist.
Let’s start off with the following overview on push notifications: http://msdn.microsoft.com/en-us/library/windows/apps/hh913756.aspx. The term Windows Push Notification Services (WNS) is defined in the link. Essentially, for all of this magic to work, we have three components that must communicate
- Client side: Receives notifications, receives URI channel, sends URI channel to cloud service
- Cloud service: assumed to be the developer’s server backend. This will push notifications using the URI channel provided to it
- WNS: set of notification APIs provided as a service from Microsoft. Sends notifications to client, receives authenticated request to push notification from cloud service
Pay close attention to the section titled “Registering your app and receiving the credentials for your cloud service.” The section starts by explaining that the developer’s app must be registered with the Windows Store Dashboard. This requires getting a developer account. The following link tells you how: http://msdn.microsoft.com/en-us/library/windows/apps/hh868184.aspx.
It isn’t necessary to complete all the dashboard steps to start sending push notifications. After I completed the minimum steps, here is what I am presented with after clicking the shown “edit” button below:
Figure 1: Dashboard Main Page (snapshot taken from appdev.microsoft.com)
Figure 1 above shows the minimum steps (marked with checks) that I needed to go through in order to complete the prerequisites for sending push notifications. For the “packages” section, Microsoft runs automatic validation on your app package. After two attempts, I managed to get validation complete after both changing my project build from debug to release along with making sure that the names defined in the project manifest file matched what was specified in the dashboard.
Once the dashboard steps are completed, Microsoft provides two key pieces of information that the developer must keep secure and accessible to the cloud service: a secret string, and an SID string. When the cloud service wishes to push notifications to client(s) via WNS, the cloud service will use these two strings to authenticate with WNS. In return, WNS provides the cloud service an access token which is used for subsequent requests until it expires. The details for authenticating your app with WNS are found here: http://msdn.microsoft.com/en-us/library/windows/apps/hh465407.aspx.
Now, once the developer has completed the above steps for the cloud service (developer’s server backend), the client proceeds with requesting a URI channel. Check out this link for a breakdown: http://msdn.microsoft.com/en-us/library/windows/apps/hh868221.aspx. To receive notifications, here is sample code for how the client requests a URI channel:
String SERVER_URL = http://my.serverURL.info.tm:8080
public static async void requestUriChannel()
PushNotificationChannel channel = null;
channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
sendChannelToServer(SERVER_URL, channel); //see below implementation
catch (Exception )
// Could not create a channel.
Figure 1: Acquiring a Channel URI (Code Based on Sample Code Provided at http://msdn.microsoft.com/en-us/library/windows/apps/hh868221.aspx )***
Once the client has received a URI channel, it sends the channel to the server (server implementation not discussed in this blog) using a HTTP POST request. Just in case, here is a good explanation regarding POST requests: http://en.wikipedia.org/wiki/POST_(HTTP). The following sample code could be an example as to how the client sends the channel URI to the cloud service:
private static async void sendChannelToServer(String serverUrl, PushNotificationChannel channel)
// Create the web request.
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
byte channelUriInBytes = System.Text.Encoding.UTF8.GetBytes("ChannelUri=" + channel.Uri);
// Write the channel URI to the request stream.
Stream requestStream = await webRequest.GetRequestStreamAsync();
requestStream.Write(channelUriInBytes, 0, channelUriInBytes.Length);
// Get the response from the server.
WebResponse response = await webRequest.GetResponseAsync();
StreamReader requestReader = new StreamReader(response.GetResponseStream());
String webResponse = requestReader.ReadToEnd();
catch (Exception )
// Could not send channel URI to server.
Figure 2: Sending the Channel URI to the Cloud Service (Code Based on Sample Code Provided at http://msdn.microsoft.com/en-us/library/windows/apps/hh868221.aspx )***
Finally, at some point, the cloud service may elect to use the provided channel URI to send out a toast notification to client(s). As one would guess, the cloud service gets this done by sending an HTTP POST request (with access token provided for authentication) to WNS. This cloud server backend could be C# code, a python script, etc. and is left as exercise to the reader.
I hope that this blog was helpful and detailed enough to get you started with push notifications for Windows Store apps. If there any questions/comments, please, don’t hesitate to reply. Thanks for reading!
***This sample source code is released under the Microsoft Limited Public License (MS-LPL) and is released under the Intel OBL Sample Source Code License (MS-LPL Compatible)