使用 VB.NET 发送多播推送通知
Posted
技术标签:
【中文标题】使用 VB.NET 发送多播推送通知【英文标题】:Send multicast push notification with VB.NET 【发布时间】:2014-11-14 17:13:20 【问题描述】:我使用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
【问题讨论】:
【参考方案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
【讨论】:
以上是关于使用 VB.NET 发送多播推送通知的主要内容,如果未能解决你的问题,请参考以下文章
使用 Google Cloud Messaging 发送广播通知