2014-11-14 130 views
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 

回答

1

我解決了這個。

的問題是在參數 「」 delay_while_idle 「」:1

此參數只接受JSON布爾,所以它必須是: 「」 delay_while_idle 「」:真或 「」 delay_while_idle 「」:假的。我決定刪除它,因爲默認值是假的,而且是我想要的。

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