Android Question new fcm receiver insert sql

epneeh

Member
Hello, i followed this tutorial migrating from fcm service to fcm receiver, yes it is working, with the app in foreground or background the notification is showing, the problem is :

sql insert:
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
   
    If sql1.IsInitialized = False Then
        sql1.Initialize(File.DirInternal,"main.db",True)
    End If
   
    fm.HandleIntent(StartingIntent)
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
   
    If checkTableexist("pesan") Then
        Dim logo As Bitmap = LoadBitmapResize(File.DirAssets, "single-icon.png", 24dip, 24dip, True)
        Dim n As NB6
        n.Initialize("default", Application.LabelName, "HIGH").SmallIcon(logo).AutoCancel(True)
        Dim cs As CSBuilder
        n.BigTextStyle(Message.GetData.Get("title"), cs.Initialize.BackgroundColor(Colors.Red).Append("").PopAll,Message.GetData.Get("body"))
        n.Build(Message.GetData.Get("title"), "", "tag",pesan).Notify(1)
        sql1.ExecNonQuery2("insert into pesan (judul,isi) values (?,?)",Array As Object(Message.GetData.Get("title"),Message.GetData.Get("body")))
    End If
End Sub

with code above i can't get the message insert into table when the apps in the background otherwise it is working fine, need assitance, thanks.
 

epneeh

Member
change:
Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    
    'If checkTableexist("pesan") Then
        Dim logo As Bitmap = LoadBitmapResize(File.DirAssets, "single-icon.png", 24dip, 24dip, True)
        Dim n As NB6
        n.Initialize("default", Application.LabelName, "HIGH").SmallIcon(logo).AutoCancel(True)
        Dim cs As CSBuilder
        n.BigTextStyle(Message.GetData.Get("title"), cs.Initialize.BackgroundColor(Colors.Red).Append("").PopAll,Message.GetData.Get("body"))
        n.Build(Message.GetData.Get("title"), "", "tag",pesan).Notify(1)
        
        If sql1.IsInitialized = False Then
            sql1.Initialize(File.DirInternal,"main.db",True)
        End If
        
        sql1.ExecNonQuery2("insert into pesan (judul,isi) values (?,?)",Array As Object(Message.GetData.Get("title"),Message.GetData.Get("body")))
    'End If
End Sub

it is still not working, im also change the checkTableexist to comment, it seem that if the app is in the background or closed, the sub fm_MessageArrived is not firing, i can tell that the log of "Message arrived" is not showed, any idea?
 
Upvote 0

walt61

Well-Known Member
Licensed User
Longtime User
I only use FCM in one app (and can only go by what I see there) and it receives the message even when in background. When my app starts (actually in my B4XMainPage.B4XPage_Created method) I do this:
B4X:
CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")

and in my FirebaseMessaging receiver, I have this method:
B4X:
Public Sub SubscribeToTopics
    fm.SubscribeToTopic("mytopic") ' You can subscribe to more topics
End Sub

Has 'fm.SubscribeToTopic' been called by your app? (That's really the only thing I can see/know about it)
 
Upvote 0

epneeh

Member
yes i have it, full code :

B4X:
Sub Process_Globals
    Private fm As FirebaseMessaging
    Private sql1 As SQL
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If FirstTime Then
        fm.Initialize("fm")
    End If
        
    fm.HandleIntent(StartingIntent)
End Sub

Public Sub SubscribeToTopics
    'Log("subs topic")
    fm.SubscribeToTopic("hadirkubroadcast") 'you can subscribe to more topics
    Do While fm.Token = ""
        Log("waiting")
        Sleep(1000)
    Loop
    'Log(fm.Token)
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    
    If sql1.IsInitialized = False Then
        sql1.Initialize(File.DirInternal,"main.db",True)
    End If
    
    If checkTableexist("pesan") Then
        Dim logo As Bitmap = LoadBitmapResize(File.DirAssets, "single-icon.png", 24dip, 24dip, True)
        Dim n As NB6
        n.Initialize("default", Application.LabelName, "HIGH").SmallIcon(logo).AutoCancel(True)
        Dim cs As CSBuilder
        n.BigTextStyle(Message.GetData.Get("title"), cs.Initialize.BackgroundColor(Colors.Red).Append("").PopAll,Message.GetData.Get("body"))
        n.Build(Message.GetData.Get("title"), "", "tag",pesan).Notify(1)
        
        sql1.ExecNonQuery2("insert into pesan (judul,isi) values (?,?)",Array As Object(Message.GetData.Get("title"),Message.GetData.Get("body")))
    End If
End Sub

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

Public Sub checkTableexist(tbname As String) As Boolean
    Dim rset As ResultSet = sql1.ExecQuery2("select name from sqlite_master where name=?",Array As String(tbname))
    Dim hasil As Boolean
    If rset.RowCount > 0 Then
        hasil = True
    Else
        hasil = False
    End If
    rset.Close
    Return hasil
End Sub

to be clear, the notification is received whether the app is closed, background or foreground, the problem is when the app is in the background or closed, i think Sub fm_MessageArrived if not fired, so it will not save data to table
 
Upvote 0

walt61

Well-Known Member
Licensed User
Longtime User
My B4A version is 13.00, app is installed on Android 12, and my manifest code looks like this:
B4X:
'Google Play Services Base - Always required when using Google Play Services or Firebase:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
'Firebase - Always required when using Firebase
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
'Firebase Notifications / Push messages
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
' Firebase Analytics
CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
/code]

And (as I needed these values and my app will never be on Google Play):

[code]
... minSdkVersion="16" android:targetSdkVersion="26" ...

The only other thing I can think of is: check your Google Play Console for error messages (can't tell you how, you'd need to check the forums here) but then I'm afraid I can't help any further and someone who knows more about it will have to chime in :)
 
Upvote 0

epneeh

Member
My B4A version is 13.00, app is installed on Android 12, and my manifest code looks like this:
B4X:
'Google Play Services Base - Always required when using Google Play Services or Firebase:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
'Firebase - Always required when using Firebase
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
'Firebase Notifications / Push messages
CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
' Firebase Analytics
CreateResourceFromFile(Macro, FirebaseAnalytics.FirebaseAnalytics)
/code]

And (as I needed these values and my app will never be on Google Play):

[code]
... minSdkVersion="16" android:targetSdkVersion="26" ...

The only other thing I can think of is: check your Google Play Console for error messages (can't tell you how, you'd need to check the forums here) but then I'm afraid I can't help any further and someone who knows more about it will have to chime in :)

thanks, there is no error, and all config is fine, i'm giving up, i have done it by fetching the message from server which the message sent, maybe this is a bug ?
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I use this code below to save the notification in a database, but I put the code in the sub fm_"MessageArrived":

B4X:
Dim m As Map
m.Initialize
m.Put("title", Message.GetData.Get("title"))
m.Put("message", Message.GetData.Get("body"))
m.Put("content", Message.GetData.Get("content"))
m.Put("topic", Message.From)
m.Put("messageid", Message.MessageId)

Dim ListOfMaps As List
ListOfMaps.Initialize
ListOfMaps.Add(m)

If Starter.SQLPush.IsInitialized = False Then
    Starter.SQLPush.Initialize(File.DirInternal,"pushdb.sdb",True)
End If

Try
    DBUtils.InsertMaps(Starter.SQLPush, "notifications", ListOfMaps)
Catch
    Log(LastException)
End Try
 
Upvote 0
Top