Android Question B4Xpages Draw on top

AndroidMadhu

Active Member
Licensed User
Hello,
I am trying to open the App when I receive any notification from FCM. Please note that I have closed my App.
FCM is working fine even when I killed the App manually.
I have given the following permission at the manifest Editor :
B4X:
AddPermission(android.permission.SYSTEM_ALERT_WINDOW)

The below is the code I have written to open the App upon receive the notification :
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)
B4XPages.ShowPage("actPict")
End Sub

But the page [actpict] is not opening.

Please advice is that possible ?

Thanks
 

AndroidMadhu

Active Member
Licensed User
You need to start the main activity with StartActivity(Main) if IsPaused(Main) = False.
@Erel.. I am able to open the main page in B4Xpages using StartActivity(Main).
But If I want to open any other pages apart from Main is there is any way to do that.
I am using B4XPages.GlobalContext , but I am not sure about how to implement the same.

The below is my code base:
B4X:
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
lock.PartialLock
working = True
ConnectAndReconnect
Dim AppGlobals As clsGlobals
AppGlobals.Initialize
B4XPages.GlobalContext = AppGlobals
End Sub

Error:
1613978362612.png



Please advice on this please....
 
Upvote 0

Marvel

Active Member
Licensed User
Another option that I think might work is having a separate activity that opens when a notification is clicked on. The activity has to load the same layout with the corresponding b4xpage you are trying to open. This way, you don't have to show any b4xpage, and clicking on the back button will close the new activity and take you back to the b4xmainpage.

I got this idea from Instagram. I think they make use of something like that. When you open any Instagram notification, for a brief moment you can see that the page title is named "Activity" before it actually loads in any data.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
This is how I have implemented something like this, hope it helps.

In my Main.Actity_Create, I have this..

B4X:
If FirstTime Then
        LogColor("Request permissions..." , xui.Color_Magenta)
        Dim c As RequestDrawOverPermission 'this is the name of the class
        c.Initialize
        Wait For (c.GetPermission) Complete (Success As Boolean)
        LogColor("Permission granted: " & Success, xui.Color_Magenta)
        '
        Dim b As Boolean = CheckForGooglePlayServices
        If b = False Then
            MsgboxAsync("Google Play Services needs to be updated!","GPS")
        End If
    End If

When the app is installed, it asks for the permission to display over other pages. This happens once for the duration of the install.

If fm_MessageArrived sub on the FirebaseMessage service, I have..

B4X:
If B4XPages.IsInitialized Then
                B4XPages.MainPage.UpdateBadge
                Dim ap As String = B4XPages.MainPage.ActivePage
                        
                Select Case ap
                Case "pg1", "MainPage"
                    StartActivity(Main)
                    B4XPages.MainPage.NatigateTo("pg1")
                Case "myloc"
                    mod.FromNotification = True
                    mod.NotificationPosition = data
                    StartActivity(Main)   
                    B4XPages.MainPage.NatigateTo("myloc")
                End Select
            End If

So depending on the message received, I show the page that I need.

Then in Main.Activity_Resume, I have

B4X:
Sub Activity_Resume
    LogColor("Main.Resume...", Colors.Magenta)
    B4XPages.Delegate.Activity_Resume
    B4XPages.MainPage.ActivityResume
End Sub

Then in the MainPage I have..

B4X:
Sub ActivityResume
    LogColor("MainPage.Activity_Resume...", xui.Color_Magenta)   
    Dim in As Intent = B4XPages.GetNativeParent(Me).GetStartingIntent
    If in.IsInitialized And in <> LastIntent Then
        LastIntent = in
        If in.HasExtra("Notification_Tag") Then
            Dim stag As String = in.GetExtra("Notification_Tag")
            mod.FromNotification = True
            mod.NotificationPosition = mod.Json2Map(stag)
            Dim smsgtype As String = mod.NotificationPosition.Get("msgtype")
            Select Case smsgtype
            Case "message", "registration"
                B4XPages.ShowPage("pg1")
            Case "08", "09", "03", "04"
                If myloc.gmap.IsInitialized = False Then
                    Wait For (myloc.InitializeMap) Complete (Success As Boolean)
                    myloc.MapReady = Success
                    If myloc.MapReady Then
                        myloc.gmap.AddMarker(0, 0, "Center")
                    End If
                End If   
                B4XPages.ShowPage("myloc")
            End Select
        Else
...

My app receives notifications, whether its running / not running, selecting the notification goes to the page I want.

Good luck
 
  • Like
Reactions: udg
Upvote 0
Top