Android Question Problem with duplicate fcm message!

Lucas Siqueira

Active Member
Licensed User
Longtime 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

  • B4A2.PNG
    B4A2.PNG
    199.8 KB · Views: 531
  • B4A.PNG
    B4A.PNG
    154.3 KB · Views: 440
  • b4j.PNG
    b4j.PNG
    179.2 KB · Views: 369
  • B4J NOTIFICACAO.zip
    156.3 KB · Views: 273
  • B4A NOTIFICACAO.zip
    326.3 KB · Views: 272

asales

Expert
Licensed User
Longtime 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
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime 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
 
Upvote 0

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
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime 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!
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime 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 :)
 
Upvote 0

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
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime 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! :)
 
Upvote 0
Top