Android Question Question with remote notifications with Firebase

Christian García S.

Active Member
Licensed User
Hello,

I have a question when receiving notifications sent through a script https://fcm.googleapis.com/fcm/send.

When the application is in the foreground the notification is received, I click on the notification and the fm_MessageArrived event is executed, but when the application is paused or off I receive notifications correctly but when I click on the notification the fm_MessageArrived event does not fire and I can´t handle any event in order to open an activity or display a message.

I've done some tests but I can not get it to work, I do not know if I'm missing something.

I am using B4 8.30, NB6, target sdk 26, I have a FirebaseMessaging service with this code:

B4X:
Sub Service_Create
    fm.Initialize("fm")
    SubscribeToTopics
End Sub

Public Sub SubscribeToTopics
    Log("Suscrito a fireBase")
    fm.SubscribeToTopic("general") 'you can subscribe to more topics   
    Globals.Token = fm.Token
    Log("Token:" & Globals.Token)
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
    Sleep(0) 'allow the MessageReceived event to be raised.
    Service.StopAutomaticForeground
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)   
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)   
End Sub

In the starter service I have this:

B4X:
Sub Service_Create
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")    
End Sub

Thank you.
 

Christian García S.

Active Member
Licensed User
Now I receive the notifications, I had a mistake in that script, I changed in json "notification" to "data", now event is raised.

In previus versions I have to notify with this code when I receveid message on fm_MessageArrived with this code:

B4X:
Sub Push2(Titulo As String, texto As String)
    Dim n As Notification
    IdNotification = IdNotification + 1
    n.Initialize
    n.Icon = "icon"
    n.Vibrate = True
    n.Sound = True
'    n.OnGoingEvent = True
    n.AutoCancel = True
    'n.SetInfo(Titulo, texto, act_notificaciones)
    n.SetInfo2(Titulo,texto, Titulo, act_notificaciones)
    n.Notify(IdNotification)
  
End Sub

Now with this version and target SDK, I received a two notifications, the first when I call sub Push2, and other notification I think is default, but the second notification has a title and Body with the name of the app, and I have two notifications and I need only one.
I dont know if I maked a mistake.

I tested in two devices in android 8 I have 2 notifications, in android 6 I have 1 notification as expected.

Thanks
 
Last edited:
Upvote 0

Anser

Well-Known Member
Licensed User
Longtime User
Ensure the following is done

  1. All the recommended updates are done via B4A SDK Manager
  2. The FirebaseMessaging service has the following code
    B4X:
    Sub Service_Start (StartingIntent As Intent)
        If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
        Sleep(0)
        Service.StopAutomaticForeground 'remove if not using B4A v8+.
    End Sub
  3. Ensure that the latest FirebaseNotifications libraries are used v1.21
  4. Manifest Entries required for firebase as follows
    B4X:
    'Always required when using Google Play Services or Firebase:
    '************ Google Play Services Base ************
    CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
    
    'Always required when using Firebase:
    CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)
    
    'Always required when using Firebase Notifications / Push messages
    CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)
I had the same issue, resolved it by following the above steps.

https://www.b4x.com/android/forum/t...-2-notifications-simultaneously-solved.94233/
 
Last edited:
Upvote 0
Top