Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am new to push notification for Windows Phone 7.
Can anyone instruct and tell me code for Windows Phone 7 server side push notification in Java.

I have tried the following code but it is not working and showing errors.


XML
String subscriptionUri="";
         HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
         sendNotificationRequest.Method = "POST";

         //Indicate that you'll send toast notifications!
         sendNotificationRequest.ContentType = "text/xml";
         sendNotificationRequest.Headers = new WebHeaderCollection();
         sendNotificationRequest.Headers.Add("X-NotificationClass", "2");

         //if (String.IsNullOrEmpty(txtMessage.Text)) return;
         //Create xml envelope
         String data = "X-WindowsPhone-Target: toast\r\n\r\n" +
                        "<?xml version='1.0' encoding='utf-8'?>" +
                        "<wp:Notification xmlns:wp='WPNotification'>" +
                        "<wp:Toast>" +
                        "<wp:Text1>{0}</wp:Text1>" +
                        "</wp:Toast>" +
                        "</wp:Notification>";

         //Wrap custom data into envelope
         //String message = String.Format(data, txtMessage.Text);
         byte[] notificationMessage = Encoding.Default.GetBytes(data);

         // Set Content Length
         sendNotificationRequest.ContentLength = notificationMessage.length;

         //Push data to stream
         using (Stream requestStream = sendNotificationRequest.GetRequestStream())
         {
             requestStream.Write(notificationMessage, 0, notificationMessage.Length);
         }

         try
         {
         //Get reponse for message sending
         HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
         String notificationStatus = response.Headers["X-NotificationStatus"];
         String notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
         String deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

         return (int) response.StatusCode;
         }
         catch (WebException ex)
         {
            return (int) ((HttpWebResponse) ex.Response).StatusCode;
         }



1)It is showing error on the line-

HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);


2)I have import following class-

C#
import org.omg.IOP.Encoding;
import java.net.*;
import java.io.*;
import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
 import javapns.data.Device;
 import javapns.data.PayLoad;




Can anyone help me in removing error or tell some other code for the same.

Thanks ane Regards.
Posted
Updated 3-May-11 23:12pm
v2
Comments
Sandeep Mewara 4-May-11 3:08am    
Tried anything?
pavankale 4-May-11 3:43am    
Ya,I have tried following code but I don't know whether it is correct or not.


String subscriptionUri="";
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
sendNotificationRequest.Method = "POST";

//Indicate that you'll send toast notifications!
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers = new WebHeaderCollection();
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");

//if (String.IsNullOrEmpty(txtMessage.Text)) return;
//Create xml envelope
String data = "X-WindowsPhone-Target: toast\r\n\r\n" +
"" +
"<wp:notification xmlns:wp="WPNotification">" +
"<wp:toast>" +
"<wp:text1>{0}" +
"" +
"";

//Wrap custom data into envelope
//String message = String.Format(data, txtMessage.Text);
byte[] notificationMessage = Encoding.Default.GetBytes(data);

// Set Content Length
sendNotificationRequest.ContentLength = notificationMessage.length;

//Push data to stream
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}

try
{
//Get reponse for message sending
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
String notificationStatus = response.Headers["X-NotificationStatus"];
String notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
String deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

return (int) response.StatusCode;
}
catch (WebException ex)
{
return (int) ((HttpWebResponse) ex.Response).StatusCode;
}




1)It is showing error on line-
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(uri);
Sandeep Mewara 4-May-11 4:27am    
Good, edit and add this to your question. Next time, always share your effort, it helps!
Nagy Vilmos 4-May-11 7:46am    
Is it a compile or runtime error?

Please provide details of the error you are receiving.
pavankale 4-May-11 8:37am    
There are errors in the code so I am not able to compile the code.I don't know whether thatcode is correct or not.
If you have any other code for server side push notification please guide me in that way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900