03 June, 2011

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 () 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 or get the source from and compile yourself.

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

using System;
using System.Net;
namespace Prowlin.UrlMonitor
{
		internal class Program
		{
			private static void Main(string[] args) {
				if (args.Length <= 0) {
					Console.WriteLine("Enter URL as parameter");
					return;
				}
				string urlToTest = args[0];
				var request = WebRequest.Create(urlToTest) as HttpWebRequest;
				WebResponse response = default(WebResponse);
				try {
						response = request.GetResponse();
				} catch (WebException webException) {
					string message = string.Empty;
					switch (webException.Status) {
						case WebExceptionStatus.Timeout
							message = "Request timed out";
							break;
						case WebExceptionStatus.ProtocolError
							var httpWebResponse = webException.Response as HttpWebResponse;
							message = httpWebResponse.StatusCode + " " + httpWebResponse.StatusDescription;
							break;
						default
							message = "Other problem";
							break;
					}
					SendProwlNotification(message, urlToTest);
				}
			}
 
			private static void SendProwlNotification(string message, string url) {
				var notification = new Notification
					{
							Application = "URL Monitor",
							Description = message,
							Event = url + " not available",
							Priority = NotificationPriority.High,
							Url = url
					};
			notification.AddApiKey("589a2d241e6ea26a11c994af835012eb3230f39f");
 
			var prowlClient = new ProwlClient();
			prowlClient.SendNotification(notification);
		}
	}
}

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.


Tags: