-
Notifications
You must be signed in to change notification settings - Fork 44
Push Service
Every platform for smartphones has its own mechanism for pushing data to the mobile application using dedicated servers. In the following we will see as an example how to use the Windows Phone one.
Using the same principle as the connectionless push remote wrapper, we can push data to the Windows Phone Push Service. Which will then be responsible to push the data to the phone. (see this page for more information). The class gsn.http.rest.WPPushDelivery implements the Delivery System interface and define the few specific parts for the windows phone push service. This is mostly generating the correct xml data and retrieving the status of the server from the response. This class is written as a generic notification, but it should be adapted for each specific mobile application.
To register for a Push notification from the phone a POST request has to be sent to the server. The following fragment of C# code initialize the push channel and register it on GSN:
public MainPage()
{
InitializeComponent();
HttpNotificationChannel channel;
string cName = "GSNChannel";
channel = HttpNotificationChannel.Find(cName);
if (channel == null)
{
channel = new HttpNotificationChannel(cName);
channel.ChannelUriUpdated += (sender, e) => { SendURI(e.ChannelUri.ToString()); };
channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred);
channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(channel_HttpNotificationReceived);
channel.Open();
}
else
{
channel.ChannelUriUpdated += (sender, e) => { SendURI(e.ChannelUri.ToString()); };
channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred);
channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(channel_HttpNotificationReceived);
SendURI(channel.ChannelUri.ToString());
}
if (!channel.IsShellToastBound)
channel.BindToShellToast();
}
public void SendURI(String suri)
{
try
{
byte[] bID = (byte[])DeviceExtendedProperties.GetValue("DeviceUniqueId");
double deviceID = BitConverter.ToDouble(bID, 12); //use the 8 last bytes as unique identifier for GSN
String query = HttpUtility.UrlEncode("select * from model_zurich_o3"); //put your query here, it can also be dynamicaly generated
System.Uri uri = new Uri("http://server/gsn/streaming/wp2Alert/" + query); //this is the GSN instance (please note the /wp3 for asking raw notifications. Use /wp2 for toast and /wp1 for tile notification!)
HttpWebRequest r = (HttpWebRequest)WebRequest.Create(uri);
r.ContentType = "application/x-www-form-urlencoded";
r.Method = "POST";
r.BeginGetRequestStream(delegate(IAsyncResult req)
{
var outStream = r.EndGetRequestStream(req);
byte[] dataStream = Encoding.UTF8.GetBytes("notification-id=" + deviceID.ToString("F") + "&local-contact-point=" + HttpUtility.UrlEncode(suri));
outStream.Write(dataStream, 0, dataStream.Length);
outStream.Close();
r.BeginGetResponse(delegate(IAsyncResult res)
{
try{
HttpWebRequest hwr = (HttpWebRequest)res.AsyncState;
HttpWebResponse hwe = (HttpWebResponse)hwr.EndGetResponse(res);
using (StreamReader httpwebStreamReader = new StreamReader(hwe.GetResponseStream()))
{
string results = httpwebStreamReader.ReadToEnd();
//handle result
}
hwe.Close();
}catch(Exception e){
//handle errors
}
},r);
}, null);
}
catch (Exception)
{
//handle errors
}
}