Skip to content
sarni edited this page Feb 24, 2014 · 5 revisions

Mobile Phones Push Services

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.

Windows Phone 7.5

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
            }
        }

In this example, the data is provided by a virtual sensor output, but one may want to run continuous queries over a model output. If you have a ModellingVirtualSensor with a model Model attached to it, you can change the line defining the URI to this one:

System.Uri uri = new Uri("http://server/gsn/streaming/wp2Alert/" + query + "/using+Model");
Clone this wiki locally