lördag 12 september 2015

Simple VB code to send message (note) to all devices with Pushbullet API.

Keeping it short, this is simple working example how to send note from VB program to your device(s) with Pushbullet.
You will need the token from Pushbullet settings page and copy it to the code.
For the test project use the following design (items are with default names):

  • Form
  • Button
  • RichTextBox

Imports needed for the code:

Imports System
Imports System.IO
Imports System.Net
Imports System.Text

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim token As String = " **** your token from Pushbullet ****"
        Dim title As String = "Title goes here"
        Dim body As String = "Message body goes here"
       

        Try
            ' Create a request using a URL that can receive a post.'
            Dim Request As HttpWebRequest = CType(WebRequest.Create("https://api.pushbullet.com/v2/pushes"), HttpWebRequest)

            ' Set the Method property of the request to POST.'
            Request.Method = "POST"

            ' Create POST data and convert it to a byte array.'
            Dim postData As String = "{""type"": ""note"", ""title"": """ & title & """, ""body"": """ & body & """}"
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

            ' Set the ContentLength property of the WebRequest.'
            Request.ContentLength = byteArray.Length
            ' Set the ContentType property of the WebRequest.'
            Request.ContentType = "application/json"
            ' Add the token to header.'
            Request.Headers.Add("Access-Token", token)
            ' Get the request stream.'
            Dim dataStream As Stream = Request.GetRequestStream()
            ' Write the data to the request stream.'
            dataStream.Write(byteArray, 0, byteArray.Length)
            ' Close the Stream object.'
            dataStream.Close()
            ' Get the response.'
            Dim response As WebResponse = Request.GetResponse()
            ' Get the stream containing content returned by the server.'
            dataStream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.'
            Dim reader As New StreamReader(dataStream)
            ' Read the content.'
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Show result in RichTextBox.'
            RichTextBox1.Text = responseFromServer

            ' Clean up the streams.'
            reader.Close()
            dataStream.Close()
            response.Close()

            ' Error handling - write to Immediate window with debug.print'
        Catch a As ArgumentException
            Debug.Print(a.Message)
        Catch a As WebException
            Debug.Print(a.Message)
            If a.Status = WebExceptionStatus.ProtocolError Then
                Debug.Print("Status Code : {0}", CType(a.Response, HttpWebResponse).StatusCode)
                Debug.Print("Status Description : {0}", CType(a.Response, HttpWebResponse).StatusDescription)
                Debug.Print("Server : {0}", CType(a.Response, HttpWebResponse).Server)
            End If
        Catch a As Exception
            Debug.Print(a.Message)
        End Try

    End Sub


References: