I used this code to send push notification and it works well. Now i'm trying to send a notification to multiple devices, but im unable to do it, im getting "400 Bad Request Error".

Here is my modified code:

Shared Function sendPush(ByVal value As String) As String

        Dim toret As String = ""

        Dim regid As String = "[""XXXXXX"",""ZZZZZZ""]"

        Dim applicationID = "YYYYYYY"
        Dim SENDER_ID = "SSSSSSS"

        Dim tRequest As WebRequest
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
        tRequest.Method = "post"
        tRequest.ContentType = " application/json"
        tRequest.Headers.Add(String.Format("Authorization: key={0}", applicationID))

        tRequest.Headers.Add(String.Format("Sender: id={0}", SENDER_ID))

        Dim postData As String = "{""collapse_key"":""score_update"",""time_to_live"":108,""delay_while_idle"":1,""data"":{""message"":""" & Convert.ToString(value) & """,""time"":""" & System.DateTime.Now.ToString() & """},""registration_ids"":" & regid & "}"
        Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(postData)
        tRequest.ContentLength = byteArray.Length

        Dim dataStream As Stream = tRequest.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)

        Dim tResponse As WebResponse = tRequest.GetResponse()

        dataStream = tResponse.GetResponseStream()

        Dim tReader As New StreamReader(dataStream)

        Dim sResponseFromServer As [String] = tReader.ReadToEnd()

        toret = sResponseFromServer

        Return toret
    End Function

Related posts

Recent Viewed