从 ASP.NET 网络服务向 android 推送通知

Push notification to android from ASP.NET webservices

请帮助我,我无法从 ASP.NET 网络服务 (VB.NET) 推送 android 的通知。 这是我的代码。

Public sApplicationID As String = "YYY....ZZZ"
Public sSENDER_ID As String = "222...555"

<WebMethod()> _
Public Function SendMessage(ByVal RegistrationID As String, ByVal Message As String) As String
    Dim regid As String = RegistrationID
    Dim tRequest As WebRequest
    tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
    tRequest.Method = "post"
    'tRequest.ContentType = " application/json"
    tRequest.ContentType = " application/x-www-form-urlencoded"
    tRequest.Headers.Add(String.Format("Authorization: key={0}", sApplicationID))

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

    Dim postData As String = "{""collapse_key"":""score_update"",""time_to_live"":108,""delay_while_idle"":1,""data"":{""message"":""" & Convert.ToString(Message) & """,""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()

    tReader.Close()
    dataStream.Close()
    tResponse.Close()

    Return sResponseFromServer
End Function

我正在测试这项服务,但我总是收到错误消息

System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()

好像认证的东西没用。根据 guide,您的 API 密钥无效:

If you receive a 401 HTTP status code, your API key is not valid

另请参阅此答案,了解使用 GCM 时出现 401 的可能原因:。指的是一个link,已经不存在了,但是看起来很全面