Android Question B4Xpages, Firebase Messaging and StartAtBoot

Kevin Hartin

Active Member
Licensed User
Longtime User
A few years back I made a GPS tracking app that started at boot.

I'm currently developing a B4Xpages app which has Firebase Messaging as a key part of it's operation, basically notifying Clients of transactions they need to immediately action (order and payment received, requiring fulfillment actions).

This works great when the App is open and in the Foreground, with the notification chiming and flashing up on the screen. However, when the App is in the background, receipt of a notification chimes, but does not appear, though it does appear in the notification bars list, however it does not get stored in a list (Main.notifications) nor does it write to the notifications.list as per the foreground activity. See following snippet;
B4X:
    Dim n3 As Map
    n3 = CreateMap("Date":DateTime.Date(DateTime.Now),"Title":Message.GetData.Get("title"),"Body":Message.GetData.Get("body"))
    Main.notifications.Add(n3)
    Log(Main.notifications.Size)
    Dim B4XSerializator1 As B4XSerializator
    Dim data() As Byte = B4XSerializator1.ConvertObjectToBytes(Main.notifications)
    File.WriteBytes(File.DirInternal, "notifications.list", data)

I notice that GPS tracking uses a Service Module while the FirebaseMessaging uses a Code Module and any references to StartAtBoot refer to Services. Is this relevant to the issue and can I run the FirebaseMessaging as a service?

How do I get the fm_MessageArrived Sub to run when a message is received in the background?

And how do I get the App to Start at Boot in order to not miss messages when the App is not running?

Thanks,
Kev

FirebaseMessaging:
Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    Log(FirstTime)
    If FirstTime Then
        fm.Initialize("fm")
    End If
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics (Topics() As Object)
    For Each topic As String In Topics
        fm.SubscribeToTopic(topic)
        Log("SUBSCRIBED: "&topic)
    Next
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
        Log("App is in the foreground. In iOS a notification will not appear while the app is in the foreground (unless UserNotificationCenter is used).")
    End If
    Dim n2 As Notification
    n2.Initialize2(n2.IMPORTANCE_HIGH)
    n2.Icon = "icon"
    n2.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n2.Notify(1)
    DateTime.DateFormat = "dd/MM/yyyy hh:mm:ss a"
    Dim n3 As Map
    n3 = CreateMap("Date":DateTime.Date(DateTime.Now),"Title":Message.GetData.Get("title"),"Body":Message.GetData.Get("body"))
    Main.notifications.Add(n3)
    Log(Main.notifications.Size)
    Dim B4XSerializator1 As B4XSerializator
    Dim data() As Byte = B4XSerializator1.ConvertObjectToBytes(Main.notifications)
    File.WriteBytes(File.DirInternal, "notifications.list", data)
End Sub

Sub fm_TokenRefresh (Token As String)
    Log("TokenRefresh: " & Token)
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top