Skip to content

Instantly share code, notes, and snippets.

@piccoloaiutante
Created November 6, 2013 16:59
Show Gist options
  • Save piccoloaiutante/7339866 to your computer and use it in GitHub Desktop.
Save piccoloaiutante/7339866 to your computer and use it in GitHub Desktop.
Test your Windows Phone 8 push notification using Scriptcs (remember to insert your push notification uri at line 6): scriptcs .\TestPush.csx -- "your test message"
using System.Net;
// Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.
// Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send
// notifications out to.
string subscriptionUri = "YOUR PUSH NOTIFICATION URL";
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
// Create an HTTPWebRequest that posts the raw notification to the Microsoft Push Notification Service.
// HTTP POST is the only method allowed to send the notification.
sendNotificationRequest.Method = "POST";
// Create the raw message.
string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<root>" +
"<Value1>"+ScriptArgs[0]+"<Value1>" +
"</root>";
// Set the notification payload to send.
byte[] notificationMessage = Encoding.Default.GetBytes(rawMessage);
// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-NotificationClass", "3");
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}
// Send the notification and get the response.
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
// Display the response from the Microsoft Push Notification Service.
// Normally, error handling code would be here. In the real world, because data connections are not always available,
// notifications may need to be throttled back if the device cannot be reached.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment