It is very easy to send push notification on Windows Phone 8. Just doing few steps.
Creating a push client to receive toast notifications:
1) Create a new Windows Phone 8 app.
2) Name the project GenexToastClient.
3) Add the following using directives to the top of the MainPage.xaml.cs file.
using Microsoft.Phone.Notification;
3) Add following code in MainPage.xaml.cs file.
public MainPage()
{
HttpNotificationChannel pushChannel;
string channelName = "ToastDemo";
pushChannel = HttpNotificationChannel.Find(channelName);
if (pushChannel == null)
{
pushChannel = new HttpNotificationChannel(channelName);
pushChannel.ChannelUriUpdated += new EventHandler(PushChannel_ChannelUriUpdated);
pushChannel.Open();
pushChannel.BindToShellToast();
}
else
{
System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
pushChannel.ChannelUriUpdated += new EventHandler(PushChannel_ChannelUriUpdated);
MessageBox.Show(String.Format("Channel Uri is {0}", pushChannel.ChannelUri.ToString()));
}
}
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
//Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}", e.ChannelUri.ToString()));
});
}
Sending a Toast Notification:
1) Create new ASP.Net web application
2) Add one button on default.aspx page and put the following code in side the button_onclick event
//Run GenexToastClient app. Windows Phone Emulator initializes, and then the app starts. After a moment or two,
//the app should display a message with the push channel URI. This URI also will be displayed in the Visual Studio debugger Output window.
//copy this URI and pass to ChannelUri variable in following code.
string ChannelUri = "http://db3.notify.live.net/throttledthirdparty/01.00/AQGshG_QxJs2QIO1OID6wTLxAgAAAAADAQAAAAQUZm52OkJCMjg1QTg1QkZDMkUxREQFBkVVTk8wMQ";
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(ChannelUri);
sendNotificationRequest.Method = "POST";
.png)
// Set the notification payload to send.
byte[] notificationMessage = Encoding.Default.GetBytes(toastMessage);
// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
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"];
3) Run web app and Press the button on default.aspx page. you will get the notification on your windows phone.
Happy Coding