Pushover Mobile Notifications in .NET

Pushover Mobile Notifications in .NET

With Pushover being recently added to ifttt as a recipe option, I’ve been sending notifications to my phone for all those time-sensitive throwaway messages that I’d otherwise have used email for.  If tomorrow’s forecast is particularly warm, it’s going to rain, or if Team GB win an medal at the Olympics.

Since they’ve got a really straightforward REST Api, I thought it might be worthwhile integrating it into my health monitoring solution for an online app of mine.  And since I haven’t posted in a while here, and the .NET sample isn’t on their web site, I thought it might be worthwhile to share it here as well.

Code block    
public class PushoverNotification
{
	public static bool Send(string message, string userKey)
	{
		try
		{
			NameValueCollection queryString = HttpUtility.ParseQueryString(string.Empty);
 
			queryString["token"] = ConfigurationManager.AppSettings.Get("ApiKey");
			queryString["user"] = userKey;
			queryString["message"] = message;
 
			var request = (HttpWebRequest)WebRequest.Create("https://api.pushover.net/1/messages");
			request.Method = "POST";
			request.Accept = "*/*";
			string postData = queryString.ToString();
			byte[] byteArray = Encoding.UTF8.GetBytes(postData);
			request.ContentType = "application/x-www-form-urlencoded";
			request.ContentLength = byteArray.Length;
			using (Stream dataStream = request.GetRequestStream())
			{
				dataStream.Write(byteArray, 0, byteArray.Length);
				dataStream.Close();
			}
			using (WebResponse response = request.GetResponse())
			{
				using (Stream dataStream = response.GetResponseStream())
				{
					using (StreamReader reader = new StreamReader(dataStream))
					{
						string responseFromServer = reader.ReadToEnd();
					}
				}
			}
			return true;
		}
		catch
		{
			return false;
		}
	}
}

Tags: , , , , , , ,

Leave a Reply?