Send Push Notification Over PushBullet

8 years 5 months ago - 8 years 5 months ago #2509 by lennysh
Figured I'd post my code on how to send push notifications from the EasyIoT Server to your devices using the free PushBullet service...

(I also posted code on this topic on doing the same thing with PushOver instead...)

Here's the code... Enjoy!
public void PushBulletSend(string type, string title, string message)
{
  try {
  	string apikey = "Put Your Special API Key Right Here";
    
	//Set the headers to accept json and send form data 
    string authEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(apikey + ":"));
	System.Net.WebClient wc = new System.Net.WebClient();
    wc.Headers[System.Net.HttpRequestHeader.UserAgent] = "EasyIoT.Server";
    wc.Headers[System.Net.HttpRequestHeader.Authorization] = string.Format("Basic {0}", authEncoded);
	wc.Headers[System.Net.HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
	wc.Headers[System.Net.HttpRequestHeader.Accept] = "application/json";
	
	//First parameters to send
	string parameters = String.Format("type={0}", type);

	//Send different parameters depending on type to push
	//https://api.pushbullet.com/api
	if (type == "note") {
	  parameters += String.Format("&title={0}&body={1}", Uri.EscapeUriString(title), Uri.EscapeUriString(message));
	} else if (type == "link") {
	  parameters += String.Format("&title={0}&url={1}", Uri.EscapeUriString(title), Uri.EscapeUriString(message));
	} else if (type == "address") {
	  parameters += String.Format("&name={0}&address={1}", Uri.EscapeUriString(title), Uri.EscapeUriString(message));
	} else if (type == "list") {
	  parameters += String.Format("&title={0}", Uri.EscapeUriString(title));
      string[] items = message.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
      foreach (string item in items) {
        parameters += String.Format("&items={0}", Uri.EscapeUriString(item));
      }
	}

    System.Net.ServicePointManager.ServerCertificateValidationCallback =new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });

	//Send the parameters and get the result
	string result = wc.UploadString("https://api.pushbullet.com/v2/pushes", parameters);
    Console.WriteLine(result);
    
  } catch (Exception ex) {
	Console.WriteLine(ex.Message);
  }
}

And here's how I'm using the code in an Automation program:
PushBulletSend("note", "EasyIoT Message", "Test message using my PushBullet Code!");

Please Log in or Create an account to join the conversation.

7 years 2 months ago #3722 by igmsoft
Hello, I'm new to this, can you be more specific, where the code is placed and how it would look notification program that vapors exceeding the set temperature?
Thanks,

Please Log in or Create an account to join the conversation.

Time to create page: 0.225 seconds

Forum latest

  • No posts to display.