So what I am trying to do is pushing notifications to windows 8 application using javascript.
So after checking this website which is the only useful website I found.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202977(v=vs.105).aspx
I understood the following:
1- You need to develop an app that request push notification service URI which will be then shared with a push client service.
(The code for this app needs nothing as you will simply copy it from the link above)
2- You need to send a notification (raw notification) using a push client service
(here appears the problem :) )
The issue that the code is for .NET files which I am not familiar with. I tried to convert the code from the .NET to HTML and .JS where I am going to send ajax request to the given URI.
However this give me a wired error and unfortunately I couldn't find an example of sending notification using .JS
So here is the original code which basically defines a simple form that will send request to the URI. The only difference between this and my code that I am using JQuery Ajax request to send
Original Code
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendRaw.aspx.cs" Inherits="SendRaw.SendRaw" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
Enter URI:</div>
<asp:TextBox ID="TextBoxUri" runat="server" Width="666px"></asp:TextBox>
<br />
<br />
Enter Value 1:<br />
<asp:TextBox ID="TextBoxValue1" runat="server"></asp:TextBox>
<br />
<br />
Enter Value 2:<br />
<asp:TextBox ID="TextBoxValue2" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="ButtonSendRaw" runat="server" onclick="ButtonSendRaw_Click"
Text="Send Raw Notification" />
<br />
<br />
Response:<br />
<asp:TextBox ID="TextBoxResponse" runat="server" Height="78px" Width="199px"></asp:TextBox>
</form>
C# (.NET)
using System.Net;
using System.IO;
using System.Text;
C# (.NET)
protected void ButtonSendRaw_Click(object sender, EventArgs e)
{
try
{
// 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 = TextBoxUri.Text.ToString();
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>" + TextBoxValue1.Text.ToString() + "<Value1>" +
"<Value2>" + TextBoxValue2.Text.ToString() + "<Value2>" +
"</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.
TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
}
catch (Exception ex)
{
TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
}
MY Code
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<script src="jquery-2.0.3.min.js"></script>
<script>
$(document).ready(function(e) {
$('#ButtonSendRaw').click(function(){
console.log("Button Pressed");
var subscriptionUri = $('#TextBoxUri').val();
var textbox1 = $('#TextBoxValue1').val();
var textbox2 = $('#TextBoxValue2').val();
var rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<root>" +
"<Value1>" + textbox1 + "<Value1>" +
"<Value2>" + textbox2 + "<Value2>" +
"</root>";
console.log("Sending Data");
$.ajax({
url: subscriptionUri,
type: "POST",
data: rawMessage,
contentType:"text/xml",
success: function(response) {
console.log('Sucess');
console.log("login| Response");
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
});
});
</script>
</head>
<body>
Enter URI:<input type="text" ID="TextBoxUri" Width="666px" /> <br />
Enter Value 1: <input type="text" ID="TextBoxValue1" /> <br />
Enter Value 2: <input ID="TextBoxValue2" /> <br />
<button id="ButtonSendRaw">Sending Data</button> <br />
Response: <input ID="TextBoxResponse" Height="78px" Width="199px" /> <br />
</body>
</html>
The error I am getting from this is:
405 (Method Not Allowed)
Where method is set automatically to OPTIONS, although I am sending a POST request
Any suggestions, thanks in advance :)