Android Question push notification service with "roles"

ThePuiu

Active Member
Licensed User
I want to use the push notification service in an application that determines user role from user name.
Depending on the role, only certain notifications should be received. I use the following code in a service:
B4X:
Sub Process_Globals
    Public fm As FirebaseMessaging
End Sub

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

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("general")
End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground
End Sub

Sub fm_MessageArrived (Message As RemoteMessage)
    Log("Message arrived")
    Log($"Message data: ${Message.GetData}"$)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo(Message.GetData.Get("title"), Message.GetData.Get("body"), Main)
    n.Notify(1)
End Sub
but the problem is that this service runs before the application determines the role of the user.
How do I set the topic to which the user is subscribed?

if i comment the line fm.SubscribeToTopic("general"), the application is still receiving notifications but these are null.

Thanks for any idea!
 

ThePuiu

Active Member
Licensed User
I change the code like this:
B4X:
Public Sub SubscribeToTopics(Topic As String)
    fm.SubscribeToTopic(Topic)
End Sub
and i try to subscribe with:
B4X:
CallSubDelayed2(FirebaseMessaging, "SubscribeToTopics","manager")
or
B4X:
CallSubDelayed2(FirebaseMessaging, "SubscribeToTopics","worker")
probably I do something wrong ... for both case or even if the app are not subscribe to any topic, it receives all the messages, no matter who they are intended for...
 

ThePuiu

Active Member
Licensed User
I do not send any messages on "general". I tried to send the messages in two ways: with a B4J application found here, on the forum, but also from an MVC application hosted on my server. The result is the same.

B4J code:
B4X:
Sub AppStart (Args() As String)
    SendMessage("manager", "myTitle", "myBody")
    StartMessageLoop
End Sub

Private 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, "id": 1)
    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")
    Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY_ISO)
End Sub
I noticed that any message sent to one of the two "manager" and "worker" topics reaches the client even though they do not refer to Sub SubscribeToTopics!
If I'm trying to send a message to a non-existent topic (eg "xxxx"), the message does not reach the client!

the application seems to save somewhere topics where it was active and responds to any of them, regardless of whether the topic is now activated or not!
 
Last edited:

ThePuiu

Active Member
Licensed User
That's how it is! Is there any way that all notifications sent to a device remain accessible? Do I have to save them locally in the order they arrive?
 
Top