Resolved Error sending FCM notification message to Android

makis_best

New member
Joined
Nov 27, 2018
Messages
4
Programming Experience
Beginner
Hi

I try to send a notification message through VB.Net window application,
to all users who use one application I created on Android platform.

The code I use is
VB.NET:
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Text
Imports Newtonsoft.Json
Imports System.Web.Script.Serialization

Public Class Form1
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Shared Function SendPushNotification() As String
        Dim response As String

        Try
            Dim serverKey As String = "AAAAQxxxxxxxxxx"
            Dim senderId As String = "xxxxxxxxxxx"
            Dim deviceId As String = "//topics/all"
            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 = "Body1",
                    .title = "Title1",
                    .sound = "Enabled"
                }
            }
            Dim serializer = 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

        Catch ex As Exception
            response = ex.Message
        End Try

        Return response
    End Function

    Private Sub btnSendNotification_Click(sender As Object, e As EventArgs) Handles btnSendNotification.Click
        tbxResponse.Clear()
        Dim response As String = SendPushNotification()
        tbxResponse.Text = response
    End Sub
End Class

But I always get one error saying
{"multicast_id":3440317137858716350,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

Can anyone help me about what I am doing wrong?

Thank you
 
I find the solution... Just need to change the line
VB.NET:
Dim deviceId As String = "//topics/general"
to
VB.NET:
Dim deviceId As String = "/topics/general"

Thank you all
 
Back
Top