Android Question FirebaseMessaging - preventing show notification when app is running

Yunus ÖZ

Member
Licensed User
Hi,
I look all example and try all of them, but always notification is coming when app is running.
I only want to show message when notification is coming if app is running

B4X:
'B4A version 8.30
'Target SDK: 26

Sub Service_Start (StartingIntent As Intent)
    FM.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
End Sub

Sub FM_MessageArrived (Message As RemoteMessage)
   Select Message.GetData.Get("key")
       Case "VOTE"
               Dim n As Notification
               n.Initialize
               n.Icon = "icon"
               n.AutoCancel=True
               Dim f As funcs
               f.Initialize
               n.SetInfo("test","test", test)
               RndSeed(DateTime.Now)
               Dim rint As Int=Rnd(1,65536)
               n.Notify(rint)
   End Select
End Sub

Please help me :(
 

DonManfred

Expert
Licensed User
Longtime User
What is the output of
B4X:
Sub FM_MessageArrived (Message As RemoteMessage)
log("Key is "&Message.GetData.Get("key"))
   Select Message.GetData.Get("key")
       Case "VOTE"
               Dim n As Notification
               n.Initialize
               n.Icon = "icon"
               n.AutoCancel=True
               Dim f As funcs
               f.Initialize
               n.SetInfo("test","test", test)
               RndSeed(DateTime.Now)
               Dim rint As Int=Rnd(1,65536)
               n.Notify(rint)
   End Select
End Sub
 
Upvote 0

Yunus ÖZ

Member
Licensed User
it is "VOTE", i'm sending it manually from server, and my problem how can i show message when app is running on foreground :(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
and my problem how can i show message when app is running on foreground :(
How is this question related to the Threads title?

You can check if your activity is running and decide to show a Notification or not.

B4X:
    If IsPaused(Main) = False Then
        ' Main is not Paused...
    Else
        ' Main is paused (in Background)
    End If
 
Upvote 0

Yunus ÖZ

Member
Licensed User
Thank you for your insteresting but i have a lot of activity, u mean that, i check all of them isPaused or not?
when i try to msgbox in "FM_MessageArrived" function, my app give me an error and stopped to run, i think the service doesnt support to showing message :(
Meanwhile the notification may com any time and in any activity
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
you also can create a poublic processs global var in main and change it to the running activity name in Activity_create and set it to an Empty tring in Activity_pause (of each activity).

Main
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public AppOpen As Object
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    AppOpen = me ' Set AppOpen to the running Activity
end sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Any other activity

B4X:
Sub Activity_Resume
    Main.AppOpen = me
end sub

You then can check in message_arrived

B4X:
    If IsPaused(Main.AppOpen) Then
        ' App in Background
       ' Sent notification....
    End If
 
Upvote 0

Yunus ÖZ

Member
Licensed User
This so long way but good for me :)

Writing to Starter that
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public currentActivity As Object
End Sub

Writing to all activities that
B4X:
Sub Activity_Resume
    Starter.currentActivity=Me
End Sub

Sub showMessage(title As String,message As String)
    Msgbox(message,title)
End Sub

and writing to FirebaseMessaging service that
B4X:
Private Sub isBG As Boolean
    Return IsPaused(Main) And IsPaused(Login) And ....
End Sub

Sub FM_MessageArrived (Message As RemoteMessage)
   Select Message.GetData.Get("key")
       Case "VOTE"
            If isBG Then
               Dim n As Notification
               n.Initialize
               n.Icon = "icon"
               n.AutoCancel=True
               n.SetInfo("test","test", test)
               RndSeed(DateTime.Now)
               Dim rint As Int=Rnd(1,65536)
               n.Notify(rint)
           Else
                CallSub3(Starter.currentActivity,"showMessage","test","test")
           End If
   End Select
End Sub
 
Upvote 0
Top