A server-side push initiator using .NET

This sample app demonstrates how you can build push technology into your enterprise apps. This sample includes a server app and a client-side push-enabled app.

The server app monitors a server IP address that you specify. If the server goes down or is unavailable, the server app sends a push message using an HTTP POST request to the BlackBerry Enterprise Server. The BlackBerry Enterprise Server then delivers the push message to the BlackBerry device. The Push Service on the device receives the push message and starts the client app, which displays the push message.

Prerequisites

You need the following to run the sample app:

Server app

The code for the server app is in the Default.aspx.cs file. The file includes the code for the UI and for monitoring the IP address that you specify.

Send a push message

To send the alert to the client app using a push message, the sendAlert() method sends an HTTP POST request to the BlackBerry Enterprise Server. The request includes the delivery parameters and the push message that's being sent. The BlackBerry Enterprise Server then delivers the push message to the client app.

The delivery parameters of the sendAlert() method include the following information:

  • Content of the push message (transmitData).
  • BlackBerry Enterprise Server host name (besServer).
  • BlackBerry Enterprise Server push port (besPushPort).
  • Email address or PIN of the app user (alertRecipient).
  • Application ID for the client app (pushAppID).
  • The URL for the BlackBerry Enterprise Server that you want to send the push message to (httpURL).

For your app, you can get the host name, push port, and URL from the BlackBerry Enterprise Server administrator. You can get the application ID from the developer of the client app.

public void sendAlert(String serverName, String serverIP)
   {
     String transmitData = "2|" + serverName + " DOWN!|ALERT!!! " 
             + serverName + " at " 
             + serverIP 
             + " appears to be DOWN!!!!|http://www.woot.com";
     String besServer = tbBesName.Text;
     String besPushPort = tbPushPort.Text;
     String alertRecipient = tbRecipient.Text;
     String pushAppID = "bb_server_notify";
     String httpURL = "http://" + besServer
                         + ":" + besPushPort
                         + "/push?DESTINATION=" + alertRecipient
                         + "&PORT=" + pushAppID
                         + "&REQUESTURI=/";
     try
     {
         HttpWebRequest HttpWReq = 
           (HttpWebRequest)WebRequest.Create(httpURL);
         HttpWReq.Method = ("POST");
         HttpWReq.Headers.Add("ContentType", "text/plain");
         byte[] data = 
           System.Text.ASCIIEncoding.UTF8.GetBytes(transmitData);
         if (data.Length <= 8192)
         {
            HttpWReq.ContentLength = data.Length;
            Stream requestStream = HttpWReq.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            HttpWebResponse HttpWRes = 
              (HttpWebResponse)HttpWReq.GetResponse();
            if (HttpWRes.StatusCode == HttpStatusCode.OK)
            {
                //write success push
                tbResults.Text += "Notified " + alertRecipient 
                     + " via PUSH, 
                   Status: SUCCESS" + Environment.NewLine;
            }
            else
            {
                //write fail push
                tbResults.Text += "Notified " + alertRecipient 
                     + " via PUSH, 
                   Status: FAILED" + Environment.NewLine;
            }
            requestStream.Close();
            HttpWRes.Close();
         }
     }
     catch (Exception err)
     {
         //deal later
         tbResults.Text += "Error: " + err.ToString() 
              + Environment.NewLine;
     }
 }

Build and publish the server app

Follow these instructions to build and publish the monitoring app on a .NET web server.

  1. In Microsoft Visual Studio, select this type of new project: Visual C#> Web> ASP.Net Empty Web Application.

    Selections to create a project.

  2. In the Name field, type ServerNotify.

  3. In the Location field, specify a location.

  4. Click OK.

  5. Copy the following server files and folders from the ServerNotify repository on your local computer to the ServerNotify project in Microsoft Visual Studio: Default.aspx, Default.aspx.cs, web.config, and the IMG folder. You can overwrite the existing files in the project.

  6. In Microsoft Visual Studio, right-click the ServerNotify project, and click Convert to Web Application if the option is available.

  7. Clean, build, and debug the project.

  8. Rebuild and publish the project to the .NET web server.

Run the sample app

Follow these instructions to send a test push message to the client-side push-enabled app, and monitor the status of the push message.

  1. Visit the Default.aspx page on the .NET web server to display the UI for the server app.

    UI for the server app

  2. Type the following information in the UI:

    • Name: The name of the server to monitor.
    • IP: The IP address of the server to monitor. Type an IP address of a server that you know is unavailable. The server app sends a push message to the client-side app indicating that the server is down.
  3. Click Add Server.

  4. Type the following additional information:

    • Notify: The email address or the PIN of the device where you want the server app to send the push message.
    • BES: The name of the BlackBerry Enterprise Server that handles push messages for the email address you specified (for example, bes500cnc.your_company.net).
    • BES Push Port: The port on the BlackBerry Enterprise Server that's used to send push messages (for example, 8080).
    • Interval: The interval in seconds that you want the server app to monitor the IP address you specified.
  5. Click Start Monitoring.

    Our server app monitors the IP address you specified. If the server is down, the server app sends a push message to the email address that you specified. The client-side app then displays the notification in a global dialog box. the server app also displays the status of the push message in the Results field of the UI.

To learn how the client-side component of this app is built, see Client app for an enterprise push app

Last modified: 2015-06-11



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus