Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to sent a google FCM notification using asp.net vb ,below is my code

What I have tried:

Public Shared Function SendNotificationFromFirebaseCloud() As String
Dim result = "-1"
Dim webAddr = "https://fcm.googleapis.com/fcm/send"
Dim httpWebRequest = CType(WebRequest.Create(webAddr), HttpWebRequest)
httpWebRequest.ContentType = "application/json"
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=AAAAXXXX_2boWklFgW9eE2UQ_CaM8KmDjQ")
httpWebRequest.Method = "POST"
Dim streamWriter = New StreamWriter(httpWebRequest.GetRequestStream)

Dim strNJson As String = "{""message"":{""topic"":""deals"",""notification"":{""body"":""View latest deals from top brands."",""title"":""Latest Deals""}}}"


streamWriter.Write(strNjson)
streamWriter.Flush()
Dim httpResponse = CType(httpWebRequest.GetResponse, HttpWebResponse)
Dim streamReader = New StreamReader(httpResponse.GetResponseStream)
result = streamReader.ReadToEnd
Return result

End Function
Posted
Updated 1-Aug-21 0:34am
v2
Comments
Member 4772857 26-Jun-18 12:37pm    
I am getting response as bad request for above code
David_Wimbley 28-Jun-18 12:18pm    
I havent looked but if you are getting bad request as the response, check your JSON and that it is formatted properly according to the API docs for google. The issue is likely on your end and since your able to get a response...the issue is highly likely in your json request.

The other likely issue is likely in your authorization/credentials/permissions.

Bad Request typically refers to invalid API endpoint or credentials issue as already pointed out.

I've used PushSharp for Push notification on several projects without any problems. You may want to check that out too: NuGet Gallery | PushSharp 4.0.10[^]
 
Share this answer
 
Dim response As String
        Dim serverKey As String = "fcm server key"
        Dim senderId As String = "sender id"
       Dim deviceId As String = "api token"

        Dim tRequest As WebRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send")
        tRequest.Method = "post"
        tRequest.ContentType = "application/json"

        Dim data = New With {
            .[to] = deviceId,
            .notification = New With {
                .body = "test meassage",
                .title = "Welcome",
                .sound = "Enabled"
            }
        }

        Dim serializer As New JavaScriptSerializer()
        Dim json = serializer.Serialize(data)
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(json)
        tRequest.Headers.Add(String.Format("Authorization: key={0}", serverKey))
        tRequest.Headers.Add(String.Format("Sender: id={0}", senderId))
        tRequest.ContentLength = byteArray.Length

        Using dataStream As Stream = tRequest.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)

            Using tResponse As WebResponse = tRequest.GetResponse()

                Using dataStreamResponse As Stream = tResponse.GetResponseStream()

                    Using tReader As StreamReader = New StreamReader(dataStreamResponse)
                        Dim sResponseFromServer As String = tReader.ReadToEnd()
                        response = sResponseFromServer
                    End Using
                End Using
            End Using
        End Using
 
Share this answer
 
Comments
CHill60 3-Aug-21 6:32am    
An uncommented code dump is not a good solution - especially to such an old question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900