Android Question Strange behavior deceiving messages

Alejandro Moyano

Member
Licensed User
Hi i trying to receive messages and always get the messages sent ok but sometimes the app receives and sometimes not.

1618412521302.png

This is the same implemented on python (my app backend is on python, i tried with the python firebase-admin package and said sucess but nothing happen.

1618412722612.png


When i use firebase-admin its even odd as when the app is not running i receive the messages with an ugly icon but when the app is running i receive an empty message:
When i use the code above its works sometimes, and sometimes not but always with a nice icon.

The service used to catch the message is:
B4X:
'THe service call "FirebaseMessaging"
#Region  Service Attributes
    #StartAtBoot: False
    
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
End Sub

Sub Service_Create
    fm.Initialize("fm")
End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general") 'you can subscribe to more topics
End Sub

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

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Mensage recibido")
    Log($"Datos del mensaje: ${Message.GetData}"$)
    Dim n26 As NB6
    Dim b As Bitmap
    b.Initialize(File.DirAssets,"icono64x64.png")
    n26.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(b)
    n26.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), "tag1", Main).Notify(4) 'It will be Main (or any other activity) instead of Me if called from a service.
End Sub

Public Sub SetApiRestToken
    REST.API.FirebaseCloudMessagingToken = fm.Token
    Log(fm.Token)
End Sub

Sub Service_Destroy

End Sub

And the starter code is this

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    REST.Start()
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
    CallSubDelayed(FirebaseMessaging, "SetApiRestToken")
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

may i using something wrong
 

Alejandro Moyano

Member
Licensed User
Check if the app is allowed to run in the background (energy savings). Your app will be stopped otherwise and can't receive messages. Please post text only in the future.

No energy saving mode, i check it, maybe something missing but i even tried adding the grant "android.permission.FOREGROUND_SERVICE" on the manifest, I really can't figurate what i'm doing wrong!
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Just send data messages (yes, there are different message types) with out title and body. Then you only get the data and throw the notification (maybe body & title interfers). See the structure here: https://www.b4x.com/android/forum/t...d-background-data-notification.73356/#content

B4X:
Dim m As Map = CreateMap("to": $"${Devtoken}"$)
Dim data As Map = CreateMap("data": datastring) ' the data you want to send
m.Put("data", data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
   ...
 
Upvote 0

Alejandro Moyano

Member
Licensed User
Just send data messages (yes, there are different message types) with out title and body. Then you only get the data and throw the notification (maybe body & title interfers). See the structure here: https://www.b4x.com/android/forum/t...d-background-data-notification.73356/#content

B4X:
Dim m As Map = CreateMap("to": $"${Devtoken}"$)
Dim data As Map = CreateMap("data": datastring) ' the data you want to send
m.Put("data", data)
   Dim jg As JSONGenerator
   jg.Initialize(m)
   Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
   ...

great its solve the null message but i figure that when the app is killed by the operative system no message is received.
 
Upvote 0

Alejandro Moyano

Member
Licensed User
The messages are shown
Really? what if the user just restarted the phone and you sent a message 5 minutes after - will it be shown?

Sometimes yes sometimes not.

What i figured is that when the "force stop" is disabled no message is received. Notifications are highly crucial for our application, no value without them.

Captura de pantalla_20210417-154757.png


I widget could be a ugly solution, i try to avoid it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What i figured is that when the "force stop" is disabled no message is received. Notifications are highly crucial for our application, no value without them.
This is the expected behavior and there is nothing that you can do about it. The app will never start by itself after it was stopped from the settings.
 
Upvote 0

Alejandro Moyano

Member
Licensed User
This is the expected behavior and there is nothing that you can do about it. The app will never start by itself after it was stopped from the settings.

Its just auto-stop after a few minutes without use, I used the setting to check if the app is running but always die after 10 o 15 minutes without use, we are developing and fintech startup made on B4X and warning notifications are very important.
 
Upvote 0
Top