Android Question FirebaseMessaging - preventing show notification when app is running

Discussion in 'Android Questions' started by Yunus ÖZ, Aug 8, 2018.

  1. Yunus ÖZ

    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

    Code:
    '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 :(
     
  2. DonManfred

    DonManfred Expert Licensed User

    What is the output of
    Code:
    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
     
  3. Yunus ÖZ

    Yunus ÖZ Member Licensed User

    dear @DonManfred
    sorry for that, but i cant understand what you mean???
     
  4. DonManfred

    DonManfred Expert Licensed User

    Add
    Code:
    log("Key is "&Message.GetData.Get("key"))
    to the FM_MessageArrived Event and check what is the log output
     
  5. Yunus ÖZ

    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 :(
     
  6. DonManfred

    DonManfred Expert Licensed User

    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.

    Code:
    If IsPaused(Main) = False Then
            
    ' Main is not Paused...
        Else
            
    ' Main is paused (in Background)
        End If
     
  7. Yunus ÖZ

    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
     
  8. DonManfred

    DonManfred Expert Licensed User

    yes
    You can not use msgbox (even the async) from a service
     
  9. Yunus ÖZ

    Yunus ÖZ Member Licensed User

  10. DonManfred

    DonManfred Expert Licensed 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
    Code:
    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

    Code:
    Sub Activity_Resume
        Main.AppOpen = me
    end sub
    You then can check in message_arrived

    Code:
    If IsPaused(Main.AppOpen) Then
            
    ' App in Background
           ' Sent notification....
        End If
     
  11. Yunus ÖZ

    Yunus ÖZ Member Licensed User

    This so long way but good for me :)

    Writing to Starter that
    Code:
    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
    Code:
    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
    Code:
    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
     
    fabricio likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice