Home > Blockchain >  How to push asynchronous notifications through PushBullet API's
How to push asynchronous notifications through PushBullet API's

Time:01-14

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

CodePudding user response:

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
  •  Tags:  
  • Related