Notifications to iPhone/iPad Using Prowl and Prowlin in .NET

This post is a little coding for fun exercise showing one way to send push notifications to your iPhone/iPad from .NET. It’s also a shameless plug for the open source .Net library I written for this, called Prowlin (https://github.com/nippe/Prowlin) in conjunction with Prowl on the iPhone.

So, to demo this in a simple and efficient way I’m going to write a simple URL monitor that tries to get an url and if the response code is something else than 200 it sends a notification to the iPhone/iPad. I’m going to write it as a simple console application. If one would like to run it as a service TopShelj (https://github.com/Topshelf/Topshelf) helps with that or a simple scheduled task might suffice.

Scene set, lets get started. The app is simple (can be improved immensely).

First, get the latest Prowlin binary from https://github.com/nippe/Prowlin/downloads or get the source from https://github.com/nippe/Prowlin and compile yourself.

Create a console application project and add a reference to Prowlin and System.Net. And off you go:

   1:  using System;
   2:  using System.Net;
   3:   
   4:  namespace Prowlin.UrlMonitor
   5:  {
   6:      internal class Program
   7:      {
   8:          private static void Main(string[] args) {
   9:              if (args.Length <= 0) {
  10:                  Console.WriteLine("Enter URL as parameter");
  11:                  return;
  12:              }
  13:              string urlToTest = args[0];
  14:              var request = WebRequest.Create(urlToTest) as HttpWebRequest;
  15:              WebResponse response = default(WebResponse);
  16:   
  17:              try {
  18:                  response = request.GetResponse();
  19:              }
  20:              catch (WebException webException) {
  21:                  string message = string.Empty;
  22:   
  23:                  switch (webException.Status) {
  24:                      case WebExceptionStatus.Timeout:
  25:                          message = "Request timed out";
  26:                          break;
  27:                      case WebExceptionStatus.ProtocolError:
  28:                          var httpWebResponse = webException.Response as HttpWebResponse;
  29:                          message = httpWebResponse.StatusCode + " " + httpWebResponse.StatusDescription;
  30:                          break;
  31:                      default:
  32:                          message = "Other problem";
  33:                          break;
  34:                  }
  35:   
  36:                  SendProwlNotification(message, urlToTest);
  37:              }
  38:          }
  39:   
  40:          private static void SendProwlNotification(string message, string url) {
  41:              var notification = new Notification
  42:                                     {
  43:                                         Application = "URL Monitor",
  44:                                         Description = message,
  45:                                         Event = url + " not available",
  46:                                         Priority = NotificationPriority.High,
  47:                                         Url = url
  48:                                     };
  49:              notification.AddApiKey("589a2d241e6ea26a11c994af835012eb3230f39f");
  50:   
  51:              var prowlClient = new ProwlClient();
  52:              prowlClient.SendNotification(notification);
  53:          }
  54:      }
  55:  }

The program takes one parameter in, an URL. I don’t check if it actually is an URL in this sample.

The body of the program (the main function) does the try-to-get-url logic and if that call (line 18 above) fail the catch block calls SendProwlNotification on line 36. The SendProwlNotification is very straight forward or at least I hope so.

Instantiate a notification object, set properties and add one or more API Keys (used by Prowl so that notifications end up on the right phone).

New up a ProwlClient and call SendNotification with the created notification. And voila!

IMG_1005

IMG_1006

IMG_1004

I’ll try to get the package up on NuGet within the coming weeks.