WonderPush in VB.NET and Access VBA

Hello,

I currently have PHP on a webserver running in order to use your push notifications.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://management-api.wonderpush.com/v1/deliveries');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
      'accessToken' => 'YOUR_ACCESS_TOKEN',
      'targetSegmentIds' => '@ALL',
      'campaignId' => '01dqh1msgec0qgb2',
      'notification' => json_encode(['alert'=>['title'=>'Dansk Super Kart besked...', 'text'=>'test', 'targetUrl'=>'']])
)));

$rawResponse = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    $response = json_decode($rawResponse, true);
    if (isset($response['success']) && $response['success'] === true) {
        echo 'Success';
    } else if (isset($response['error']['status'])
               && isset($response['error']['code'])
               && isset($response['error']['message'])) {
        echo 'Error ' . $response['error']['status']
          . ' code ' . $response['error']['code']
          . ': ' . $response['error']['message'];
    } else {
        echo 'Error: ' . $rawResponse;
    }
}

curl_close($ch);

I’m looking in to using push notifications in VB.NET and Access VBA. Do you have any examples in using it in those environments, or could you help me translate the above example to either of those languages?

Regards
Michael Larsen

Hi Michael,

Looking at an example I found on the Internet, here’s a VB.NET attempt.
Disclaimer: I have no experience in these languages and have not been able to test it.

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

Namespace Examples.System.Net
    Public Class WebRequestPostExample
        Public Shared Sub Main()
            ' Create a request using a URL that can receive a post.   
            Dim request As WebRequest = WebRequest.Create("https://management-api.wonderpush.com/v1/deliveries")
            ' 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 Dictionary<string, Object> = New Dictionary<string, Object> {
                {"accessToken", "YOUR_ACCESS_TOKEN"},
                {"targetSegmentIds", "@ALL"},
                {"campaignId", "01dqh1msgec0qgb2"},
                {"notification", New Dictionary<string, Object> {
                    {"alert", New Dictionary<string, Object> {
                        {"title", "Dansk Super Kart besked..."},
                        {"text", "test"},
                    }},
                }},
            };
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(postData))

            ' Set the ContentType property of the WebRequest.  
            request.ContentType = "application/json"
            ' Set the ContentLength property of the WebRequest.  
            request.ContentLength = byteArray.Length

            ' 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()
            ' Display the status.  
            Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)

            ' Get the stream containing content returned by the server.  
            ' The using block ensures the stream is automatically closed.
            Using dataStream1 As Stream = response.GetResponseStream()
                ' Open the stream using a StreamReader for easy access.  
                Dim reader As New StreamReader(dataStream1)
                ' Read the content.  
                Dim responseFromServer As String = reader.ReadToEnd()
                ' Display the content.  
                Console.WriteLine(responseFromServer)
            End Using

            ' Clean up the response.  
            response.Close()
        End Sub
    End Class
End Namespace

The body of the Main sub should work more or less directly with few modifications in VBA.

Best,