Google Cloud Messaging Server in Visual Basic

walterf25

Expert
Licensed User
Longtime User
Hello everyone, i was wondering if anyone here has had any experience with GCM, i'm working on an app for a friend of mine, and i need to implement a GCM service, but i have been searching all over the place for a way to implement the server in Visual Studio/Visual Basic 2010, I have found a few examples in C# and asp.net and i have tried converting the code to plain visual basic but i keep getting a invalid registration error.

I have read the gcm documentation over and over and i can't figure out how to make it work, hopefully someone can help me put this puzzle together.
This is the code i found in C# can someone help me translate it to simple visual basic 2010

B4X:
  using (var wc = new WebClient())
        {
            wc.Headers.Add("Authorization", "key=" + "key");
            var nameValues = new NameValueCollection
                     {
                         {"registration_id", "id"},
                         {"collapse_key", Guid.NewGuid().ToString()},
                         {"data.payload", "works!!!!!!"},
                         {"message", "YES it works!!!"}
                     };
            var resp = wc.UploadValues("https://android.googleapis.com/gcm/send",nameValues);
            var respMessage = Encoding.Default.GetString(resp);
            MessageBox.Show("Got respose from GCM: " + respMessage);
        }


And this is the test code i'm trying, but i keep and error
B4X:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hc As WebRequest
        Dim collapseKey As String
        Dim postData As String
        ' Dim postData1 As String
        ' Dim postData2 As String
        Dim DeviceID As String = "799750522480"
        Dim sCommand As String = "This is a Test"
        Dim byteArray As Array
        Dim dataStream As IO.Stream

        hc = WebRequest.Create("https://android.googleapis.com/gcm/send")
        hc.Method = "post"
        hc.ContentType = "application/x-www-form-urlencoded"
        hc.Headers.Add(String.Format("Authorization:Key={0}", "AIzaSyACz_BoIsLCn6TKUWdolrDlEa0-NsGqKWQ"))
        'txtResult.Text = String.Format("Authorization: Key={0}", "AIzaSyACz_BoIsLCn6TKUWdolrDlEa0-NsGqKWQ")
        collapseKey = Guid.NewGuid().ToString("n")
        postData = String.Format("registration_id={0}&data.payload={1}&collapse_key{2}", DeviceID, sCommand, collapseKey) '&data.payload=&collpase_key=", DeviceID, sCommand, collapseKey)
        'postData1 = String.Format("&data.payload=This is just a test")
        ' postData2 = ("&collapse_key=" & collapseKey)
        'postData1 = String.Format(
        ' txtResult.Text = postData '+ postData1 + postData2
        byteArray = Encoding.UTF8.GetBytes(postData)
        hc.ContentLength = byteArray.Length

        dataStream = hc.GetRequestStream
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' dataStream.Close()

        Dim tResponse As WebResponse

        tResponse = hc.GetResponse
        dataStream = tResponse.GetResponseStream
        Dim treader As StreamReader
        treader = New StreamReader(dataStream)
        Dim response As String

        response = treader.ReadToEnd
        txtResult.Text = response '& vbCrLf & postData + postData1 + postData2
        treader.Close()
        dataStream.Close()
        tResponse.Close()

    End Sub

Invalid Registration error


I've tried using an online converter but it still doesn't work

Any and all help will be greatly appreciated!

Thank,
Walter
 
Last edited:
Top