Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to push asynchronous notifications through PushBullet API's

I’m using the app PushBullet in order to send push notifications to my phone.
Online I’ve found the only example in vb.net which is working greats but when I run it, the form is freezing.

Private Sub push(body As String)
         Dim token As String = "TOKEN"
    
         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"", ""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()
             ' Clean up the streams.'
             reader.Close()
             dataStream.Close()
             response.Close()
    
         Catch ex As Exception
         End Try
     End Sub```

It does its job but when I press the button, the form freezer for a while. So, I want it to run asynchronous. Is that possible without modifying that much the code?
Thanks

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use Async before sub then for instance rather than GetResponseStream use await GetResponseAsync, ReadToEnd can be replaced with ReadToEndAsync.

Like the following:

 Private Async Sub push(body As String)
        Dim token As String = "your token"

        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"", ""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 = Await Request.GetRequestStreamAsync()
            ' 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 = Await Request.GetResponseAsync()
            ' 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 = Await reader.ReadToEndAsync()

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

        Catch ex As Exception
        End Try
    End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading