1
我用this的代碼發送推送通知,效果很好。現在我試圖發送通知給多個設備,但即時通訊無法做到,即時獲得「400錯誤請求錯誤」。用VB.NET發送組播推送通知
這是我修改後的代碼:
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 & "}"
Console.WriteLine(postData)
Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(postData)
tRequest.ContentLength = byteArray.Length
Dim dataStream As Stream = tRequest.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim tResponse As WebResponse = tRequest.GetResponse()
dataStream = tResponse.GetResponseStream()
Dim tReader As New StreamReader(dataStream)
Dim sResponseFromServer As [String] = tReader.ReadToEnd()
toret = sResponseFromServer
tReader.Close()
dataStream.Close()
tResponse.Close()
Return toret
End Function