Android Question Filtering Push Notification in service class ....

Yayou49

Active Member
Licensed User
Hi all,

I've just implemented the Firebase Push Notification whith a great help from Erel and DonManfred !

Now, my purpose is to filter notifications by "User", but not on Firebase side, but on apk side.

I explain:

My apk runs with a central SQL database.
All users are known, in this database, by a unique UserId

When apk is running, I know everytime which user is logged with a global variable.
But global variable are not reachable everywhere (and I think not by the fm service).

So my idea was to do something in 3 steps:
- on the first apk login, I store this UserId in a txt file
- when sending the push notification, I add in the body a tag to identify the user:

"$ALL$" for all users
"$USER$" + UserId for one specifique user

- on firebase_MessageArrived Sub, I read the txt file to know if user is involved or not.

Unfortunately, I think some part of my code are not compatible with a service ....
Result is apk crashes directly.

So, according with my code below, do you think something is possible ?

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    
    Dim Title As String = Message.GetData.Get("title")
    Dim Body As String = Message.GetData.Get("body")
    Dim User As String = ""
    
    If File.Exists(File.DirInternal, "user.txt") = True Then
        User = File.ReadString(File.DirInternal, "user.txt")
    
        If Body.Contains("$ALL$") Then
        
            Body.Replace("$ALL$","")
    
            Dim n As Notification
            n.Initialize
            n.Icon = "icon"
            n.SetInfo(Title, Body, "")
            n.Notify(1)
    
        Else
    
            If Body.Contains("$USER$" + User) Then
            
                Body.Replace("$USER$" + User,"")
            
                Dim n As Notification
                n.Initialize
                n.Icon = "icon"
                n.SetInfo(Title, Body, "")
                n.Notify(1)
            End If
        End If
    End If
End Sub

All good ideas are welcome !
 

DonManfred

Expert
Licensed User
Longtime User
Asuming you have 10.000 Users.
With your "logic" you would send 10.000 Pushes just to reach ONE user? The one with id 1. 9999 times your app runs on the 9999 devices just to find out that the message is "not for me"... Only one app do find a matching id...

Think over your logic and discard the faulty system (faulty because of sending 10000 messages to reach one user.)

Your case would be even more worse if you are having for ex 1 millions users.
Sending out 999999 messages for nothing, 1 message to the right destination.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If you know the id of a user from your app. Send the firebasetoken together with the id to your server. Register a topic with the userid on the device.
As you are sending the token/userid pairs to your server you know the Token for each userid. Send only one message. The one with the token corresponding to the userid. Only ONE Message is send. The one to the right device. No need to filter anything! No need to send 9999 unnessary messages!
 
Upvote 0
Top