Android Question Problem with duplicate fcm message!

Lucas Siqueira

Active Member
Licensed User
Hello everyone!

Problem with duplicate fcm message!

I'm having problems with firebase cloud message, when I send message, I get two on mobile, one I sent and another as if it was automatic ...

My version of b4a is the latest (9.50), FirebaseNotifications version is (1.21) ...

On the firebase website I found that there are two types of messages: Notification message and Data message ... (https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages)

I have tried in every way to make the data message work, but none of the times I was unsuccessful :(
 

Attachments

asales

Well-Known Member
Licensed User
Try this in the sub Service_Start:

B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
    
    '*** INSERT THIS LINES:
    Sleep(0)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub
 

DonManfred

Expert
Licensed User
I'm using b4j to send notifications, the source is attached ...
but the call to firebase is different from the b4j project fronm the tutorial!?
Means this: "restricted_package_name":pACKAGE_APP

https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/
you b4a app does not match the code from the tutorial. Update your b4a code. Especially firebaseMessaging Service.

This is the code from the B4J App from the tutorial:

B4X:
rivate Sub SendMessage(Topic As String, Title As String, Body As String)
    Dim Job As HttpJob
    Job.Initialize("fcm", Me)
    Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
    Dim data As Map = CreateMap("title": Title, "body": Body)
    If Topic.StartsWith("ios_") Then
        Dim iosalert As Map =  CreateMap("title": Title, "body": Body, "sound": "default")
        m.Put("notification", iosalert)
        m.Put("priority", 10)
    End If
    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)
End Sub
 

Brandsum

Well-Known Member
Licensed User
I'm using b4j to send notifications, the source is attached ...
Try to change firebase service start sub code to this
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub
 

Lucas Siqueira

Active Member
Licensed User
Try to change firebase service start sub code to this
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub
thank you all! That was the solution to my duplicate messaging problem! Many thanks to everyone who helped me!
 

Lucas Siqueira

Active Member
Licensed User
Try to change firebase service start sub code to this
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub

Thanks, it worked!
Can you explain what this code does?
I didn't get it right, but it works :)
 

Brandsum

Well-Known Member
Licensed User
As per your previous code when the firebase service was receiving the payload it was starting to handle the intent in foreground mode. But after handling, you were not stopping foreground mode.
B4X:
If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
But it should be like
  • if the service starting intent is initialized then handle the intent
    B4X:
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
  • after that close the service foreground mode
    B4X:
    Service.StopAutomaticForeground
 

Lucas Siqueira

Active Member
Licensed User
As per your previous code when the firebase service was receiving the payload it was starting to handle the intent in foreground mode. But after handling, you were not stopping foreground mode.
B4X:
If StartingIntent.IsInitialized And fm.HandleIntent(StartingIntent) Then Return
But it should be like
  • if the service starting intent is initialized then handle the intent
    B4X:
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
  • after that close the service foreground mode
    B4X:
    Service.StopAutomaticForeground
Thanks! :)
 
Top