0

Please help me, I have a problem to push notification for android from ASP.NET webservice (VB.NET). this is my code.

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

I was testing this service but I always getting error message

System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
Ishaq
  • 41
  • 6
  • possible duplicate of [HTTP response code 401 in Google GCM](http://stackoverflow.com/questions/11900855/http-response-code-401-in-google-gcm) – Kariem Jun 11 '15 at 11:13

1 Answers1

0

Seems as if something with the authentication did not work. According to the guide, your API key is not valid:

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

See also this answer for possible causes for 401 when using GCM: https://stackoverflow.com/a/11903863. It refers to a link that does not exist anymore, but seems very comprehensive.

Community
  • 1
  • 1
Kariem
  • 4,398
  • 3
  • 44
  • 73
  • Terima kasih atas jawaban anda, but I still not running success. I was try and I get difference result. if i using this code : Dim postData As String = (Convert.ToString((Convert.ToString("collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=") & message) + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=") & registrationid) + "" error message: Error=MismatchSenderId – Ishaq Jun 11 '15 at 12:47
  • if i using this code : 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"":" & registrationid & "}" error message : MissingRegistration – Ishaq Jun 11 '15 at 12:52
  • 1
    Please refer to [this table](https://developers.google.com/cloud-messaging/server-ref#error-codes) for the reasons you are receiving those other error codes. Like @Kariem said, your 401 Unauthorized issue is an API key issue (your sApplicationID variable). Make sure you set it up correctly in your Developers Console (i.e. API key is a server key). – Koh Jun 11 '15 at 16:29