Android Question Is my app open?

jvrh_1

Active Member
Licensed User
I need know if my app is open.
I wish to do: When I recibed a notification...

If my app is closed: open it.
If my app is open (the user is using it): Show a toastmessage or similiar.

How can I detect if my app is open when I recibed a notification in a service module?
Thanks a lot.
 

LucasHeer

Active Member
Licensed User
Longtime User
This has always worked for me:
B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    
    If(Message.GetData.Get("title") = Null) Then 'in app
        
        Dim objMap As Map
        objMap.Initialize
        objMap.Put("d_title", Message.GetData.Get("d_title"))
        objMap.Put("d_body", Message.GetData.Get("d_body")) 'also message body
        objMap.Put("d_threadId", Message.GetData.Get("d_threadId"))
        objMap.Put("d_newsId", Message.GetData.Get("d_newsId"))
            
        CallSub2(Main, "inAppAction", objMap)
        
            
    Else
    
        Dim n As Notification
        Dim myTitle As String = Message.GetData.Get("title")
        myTitle=myTitle.ToLowerCase()
        If(myTitle.contains("message")) Then
            n.Initialize
            n.Icon = "icon"
            n.SetInfo2(myTitle, Message.GetData.Get("body"), "",  Main)
            n.AutoCancel = True
            
            n.Notify(888)
            
            
        End If
        
        
    End If
End Sub


My title/body variables are ALWAYS null whenever the app is open. I'm not sure if this is the right way to do this, but I've done it this way in 3 apps.

d_title and d_body were created by me.

I had to shove the title/body variables inside of the data parameters as d_title/d_body in order to even access those parameters whenever the app is open.
 
Upvote 0

jvrh_1

Active Member
Licensed User
But always (if the app is open or not) Message.GetData.Get ("title") is Null. This is my code:

If(Message.GetData.Get("title") = Null) Then 'If my app is open, open an especific activity

Main.NOTIFICACION=men
StartActivity(RECPECION_MENSAJES)

Else 'If my app is closed, show the notification

n.SetInfo(Message.GetData.Get("title"), men, Main)
n.Notify(1)

End If

Any idea. Thanks a lot.
 
Upvote 0

jvrh_1

Active Member
Licensed User
SOLVEEEEED. This works

B4X:
        Dim NOM As String
        Dim ActMan As JavaObject
        Dim r As Reflector
        r.Target=r.GetContext
        ActMan = r.RunMethod2("getSystemService","activity","java.lang.String")
        Dim TaskInfo As JavaObject = ActMan.RunMethod("getRunningTasks",Array As Object(1))
        Dim CompInfo As JavaObject = TaskInfo.RunMethodJO("get",Array As Object(0)).GetField("topActivity")
        NOM  = CompInfo.RunMethod("getPackageName",Null)
        Log(NOMBRE)
                       
        If NOM<>"PUT HERE YOUR PACKAGE NAME" Then
            n.SetInfo(Message.GetData.Get("title"),YOUR BODY MESAJE, Main)
            n.Notify(1)
        Else
            Main.NOTIFICACION='YOUR BODY MESAJE'
            StartActivity(PUT HERE YOUR ACTIVITY)
        End If

Thanks a lot. Thanks for your attention.
 
Upvote 0
Top