Android Question [Solved] FCM push messages and multiple apps in the same Firebase project

asales

Expert
Licensed User
Longtime User
I started to use FCM in my apps and I have a Firebase project with severals apps registered.

I want to add push notification and I want to be able to distinguish which app should received the push notification.

In the Firebase console I can select the app to received the notification.

Is possible to do this using the B4J code (not using the segment by topic)?
https://www.b4x.com/android/forum/t...s-firebase-cloud-messaging-fcm.67716/#content
 

DonManfred

Expert
Licensed User
Longtime User
Is possible to do this using the B4J code (not using the segment by topic)?

https://firebase.google.com/docs/cloud-messaging/http-server-ref
Based on the Docu above i guess it should work like this (Android only!):
B4X:
Private Sub SendMessage(Topic As String, Title As String, Body As String, destinationpackagename As String)
   Dim Job As HttpJob
   Job.Initialize("fcm", Me)
   Dim m As Map = CreateMap("to": $"/topics/${Topic}"$,"restricted_package_name":$"${destinationpackagename}"$)
   Dim data As Map = CreateMap("title": Title, "body": Body)
   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

I did not tried it though.
 
Last edited:
Upvote 0

asales

Expert
Licensed User
Longtime User
Don't works (the message is sent without errors, but not received in device).

Thanks for your tip, @DonManfred.
I will check the documentation.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
Don't works (the message is sent without errors, but not received in device).

Thanks for your tip, @DonManfred.
I will check the documentation.
According to the google documentation:
restricted_package_name (Android only)
Optional, string
This parameter specifies the package name of the application where the registration tokens must match in order to receive the message.
And:
registration_ids
Optional, array of strings
This parameter specifies the recipient of a multicast message, a message sent to more than one registration token.

The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 registration tokens. To send a message to a single device, use the to parameter.
So, I think, that you have to add in your message registration_ids and registration tokens https://stackoverflow.com/questions/42930394/how-to-get-registration-ids-from-firebase-fcm?rq=1
Registration token
A unique token string that identifies each client app instance. The registration token is required for single device and device group messaging. Note that registration tokens must be kept secret.
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Problem solved!

Along with this code:
B4X:
"restricted_package_name":$"${destinationpackagename}"$
I changed the "fm" variable name in FirebaseMessaging module of every app:
B4X:
Private fm As FirebaseMessaging
to
B4X:
Private fm2 As FirebaseMessaging
 
Upvote 0
Top