我使用this代码发送推送通知,效果很好。现在我正在尝试向多个设备发送通知,但我无法做到这一点,我得到“400 Bad Request Error”。
这是我修改后的代码:
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
答案 0 :(得分:1)
我解决了这个问题。
问题出在参数:“”delay_while_idle“”:1
此参数仅接受JSON布尔值,因此必须为:“”delay_while_idle“”:true或“”delay_while_idle“”:false。我决定删除它,因为默认值为false,这就是我想要的。
Here是完整的参数列表。
我在这里发布了未来复制粘贴的工作代码:
Shared Function sendPush(ByVal value As String) As String
Dim toret As String = ""
'Change these values
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,""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