Android Question Detect if app Open with firebase notification

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am beginning to use firebase notifications in my app and everything seems to be working.

The following code is what I am using to display the notification as it arrived:

B4X:
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

Problem is that it shows and displays the notification sound while the app is open.

Is there a way to detect if the app is open or closed and only display and play the notification sound if the app is closed only?
 

DonManfred

Expert
Licensed User
Longtime User
It is up to you to check if the app is already running in the fm_MessageArrived. If so then you are free to NOT raise the Notification. You can call a special sub in your activity which then sets or unset a global flag to indicate started by user or message arrived.

Or do i understand your question wrong?
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
You can call a special sub in your activity which then sets or unset a global flag to indicate started by user or message arrived.
Yeah, I thought that would be the case.

I guess that is pretty much the only way.

I guess I can use:

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
  
    If IsPaused(Activity1) = False Then Return True
    If IsPaused(Activity2) = False Then Return True
    ' every time I add a new Activity to my app I can add it here..
  
    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

This way if one of the activity's is visible then it will return true and stop it from running. If none is not visible then it will display the notification.
 
Upvote 0
Top