Android Question Send Notification To Android Using Visual Basic 2008

catra

New Member
Help Me, I need Tutorial and Example Project Send Notification To Android Using Visual Basic 2008
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
Well you can start by entering a search on this site by entering the search term "Send Notification" at the right upper corner of a page. Read 6 pages of give result and you will get a idea what is possible with B4A. You need to search a Visual Basic 2008 solution somewhere else and match those two solutions and hopes that it works. You can try to use a Notification example between B4A and B4J first and later try to port B4J part to Visual Basic 2008. Be aware of the fact that programming in Visual Basic is simulair then B4J, however programming in Android (B4A) differs a lot with VB programming.
 
Upvote 0

catra

New Member
Where i type this code
On class or on windows form

Imports System.Net
Imports Newtonsoft.Json

Public Class Notification

Public Sub SendNotification(ByVal deviceIDList As List(Of String), ByVal title As String, ByVal bodyMsg As String)

Dim fcmPath As String = "https://fcm.googleapis.com/fcm/send"
Dim serverKey As String = "AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoCAeI"
Dim senderID As String = "35xxxxxxx37"

Dim request As HttpWebRequest = CType(HttpWebRequest.Create(fcmPath), HttpWebRequest)
With request
.Method = "POST"
.ContentType = "application/json"
.Headers.Add(String.Format("Authorization: key={0}", serverKey))
.Headers.Add(String.Format("Sender: id={0}", senderID))
End With

Using streamWriter = New StreamWriter(request.GetRequestStream())
Dim webObject As New WebRequestFcmData
With webObject
.registration_ids = deviceIDList
.notification.title = title
.notification.body = bodyMsg
.notification.content_available = True
.notification.sound = "default"
.notification.priority = "high"
End With
Dim body = JsonConvert.SerializeObject(webObject)
With streamWriter
.Write(body)
.Flush()
.Close()
End With
End Using
Dim httpResponse = CType(request.GetResponse(), HttpWebResponse)
Using streamReader As New StreamReader(httpResponse.GetResponseStream)
Dim result = streamReader.ReadToEnd
End Using
End Sub
End Class

Public Class WebRequestFcmData
Public Property registration_ids As List(Of String)
Public Property notification As New NotificationData
End Class

Public Class NotificationData
Public Property body As String
Public Property content_available As Boolean
Public Property priority As String
Public Property title As String
Public Property sound As String
End Class
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
Help Me, I need Tutorial and Example Project Send Notification To Android Using Visual Basic 2008

Hi,
Here's a sample code :

B4X:
Function SendAndroidSimpleFCMMEssage(ByVal Topic As String, ByVal Title As String, ByVal Body As String, ByVal ID As Integer) As Boolean
        Dim HTTPClient As New HttpClient
        Dim sKey As String = String.Empty

        sKey = "YOUR_KEY"

        Const QUOTE = """"

        Dim js As New JavaScriptSerializer()

        Dim Data As String = ""

        Dim sID As String = String.Empty

        sID = ID

        Data = "{'priority':'high', 'to':'\/topics\/$TOPIC$','data':{'title':'$TITLE$','body':'$BODY$','id':'$ID$'}}".Replace("$TOPIC$", Topic).Replace("$TITLE$", (Title.Replace("'", String.Empty).Replace(QUOTE, String.Empty))).Replace("$BODY$", (Body.Replace("'", String.Empty).Replace(QUOTE, String.Empty))).Replace("$ID$", sID)

        Data = Data.Replace("'", QUOTE)

        Dim Request As New HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send")
        Request.Content = New StringContent(Data, System.Text.Encoding.UTF8, "application/json")

        HTTPClient.BaseAddress = New Uri("https://fcm.googleapis.com/fcm/send")
        HTTPClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("key", "=" & sKey)

        Dim result = HTTPClient.SendAsync(Request).Result
        Dim oResult = result.Content.ReadAsStringAsync
        If oResult.Result.Contains("message_id") Then
            Return True
        Else
            Return False
        End If
    End Function
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Firebase.vb is a class file. Just add into a project and call the method as in the example.
Let say you have a button name BtnNotify to send notification with title “Foo” and message body “Bar” and your android device subscribed to topic “and_topic-foobar”.
B4X:
    Private Sub BtnNotify_Click(sender As Object, e As EventArgs) Handles BtnNotify.Click
        Dim n As New Firebase.PushNotification
        n.SendNotification("/topics/and_topic-foobar", "Foo", "Bar")
    End Sub
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Where i type this code
On class or on windows form

Imports System.Net
Imports Newtonsoft.Json

Public Class Notification

Public Sub SendNotification(ByVal deviceIDList As List(Of String), ByVal title As String, ByVal bodyMsg As String)

Dim fcmPath As String = "https://fcm.googleapis.com/fcm/send"
Dim serverKey As String = "AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoCAeI"
Dim senderID As String = "35xxxxxxx37"

Dim request As HttpWebRequest = CType(HttpWebRequest.Create(fcmPath), HttpWebRequest)
With request
.Method = "POST"
.ContentType = "application/json"
.Headers.Add(String.Format("Authorization: key={0}", serverKey))
.Headers.Add(String.Format("Sender: id={0}", senderID))
End With

Using streamWriter = New StreamWriter(request.GetRequestStream())
Dim webObject As New WebRequestFcmData
With webObject
.registration_ids = deviceIDList
.notification.title = title
.notification.body = bodyMsg
.notification.content_available = True
.notification.sound = "default"
.notification.priority = "high"
End With
Dim body = JsonConvert.SerializeObject(webObject)
With streamWriter
.Write(body)
.Flush()
.Close()
End With
End Using
Dim httpResponse = CType(request.GetResponse(), HttpWebResponse)
Using streamReader As New StreamReader(httpResponse.GetResponseStream)
Dim result = streamReader.ReadToEnd
End Using
End Sub
End Class

Public Class WebRequestFcmData
Public Property registration_ids As List(Of String)
Public Property notification As New NotificationData
End Class

Public Class NotificationData
Public Property body As String
Public Property content_available As Boolean
Public Property priority As String
Public Property title As String
Public Property sound As String
End Class
Don’t use this class because this is sending notifications to device id. It is simpler to use topic.
 
Upvote 0
Top