Android Question Start B4XPage from Receiver (SOLVED)

walterf25

Expert
Licensed User
Longtime User
Hi all, I have an app that schedules a notification for the user, the notification has two action buttons, "Check In" and "Close", I have a receiver that captures these actions in the starting intent, based on which of these two buttons are pressed when the user opens the notification, I need to open a B4XPage named "CheckIn", I'm not very familiar how to handle this on a B4XPages project, I know with a regular Activities project, I would normally just call StartActivity(CheckIn).

How can I accomplish the same with a B4XPages project, I have tried the following...
B4X:
            B4XPages.ShowPageAndRemovePreviousPages("CheckIn")
            B4XPages.GetPage("CheckIn").As(CheckIn).AnimateFromRight
But this doesn't open the page, in addition I have added the "android.permission.SYSTEM_ALERT_WINDOW" permission and handle the permission request like this

Request Permission:
Public Sub GetPermission As ResumableSub
    If phone.SdkVersion >= 23 Then
        Dim settings As JavaObject
        settings.InitializeStatic("android.provider.Settings")
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        If settings.RunMethod("canDrawOverlays", Array(ctxt)) = True Then
            Return True
        End If
        Dim i As Intent
        i.Initialize("android.settings.action.MANAGE_OVERLAY_PERMISSION", "package:" & Application.PackageName)
        StartActivityForResult(i)
        Wait For ion_Event (MethodName As String, Args() As Object)
        Return settings.RunMethod("canDrawOverlays", Array(ctxt))
    Else
        Return True
    End If
End Sub

Private Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Private Sub GetBA As Object
    Dim jo As JavaObject = Me
    Return jo.RunMethod("getBA", Null)
End Sub

But this still doesn't open the CheckIn B4XPage, If I click on the notification itself, then the B4XMainPage is opened, but that's not the action I require.

How can I get the CheckIn Page to open directly when the user pressed on the "CheckIn" Action button on the notification?

Here's the code where the notification gets created:

Notification Creation:
Sub Process_Globals
    Private icon As Bitmap
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    Log("Starting intent: " & StartingIntent)
    icon.Initialize(File.DirAssets, "ellustr_24x24.png")
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "DEFAULT").SmallIcon(icon)
    n.AddButtonAction(icon, "Check In", MyReceiver, "Check In")
    Dim cs As CSBuilder
    n.AddButtonAction(Null, cs.Initialize.Color(Colors.Red).Bold.Append("Close").PopAll, MyReceiver, "Close")
    n.DeleteAction(MyReceiver, "Delete")
    n.Build("14 day check up", "This is your 2 week check in, would you like to review your posture?", "tag", Main).Notify(1)
End Sub

And here's the code from MyReceiver that captures the actions from the notification:

MyReceiver:
'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    If StartingIntent.IsInitialized Then
        Dim cs As CSBuilder
        cs.Initialize.Bold.Size(20).Append($"Action: ${StartingIntent.Action}"$).PopAll
        Log(cs)
        If StartingIntent.Action = "Check In" Then
            B4XPages.ShowPageAndRemovePreviousPages("CheckIn")
           B4XPages.GetPage("CheckIn").As(CheckIn).AnimateFromRight
        End If
        ToastMessageShow(cs, True)
    End If
End Sub

What am I missing?
 
Solution
Example based on NB6 example is attached.

1. You need the draw on top permission.
2. The interesting code is in the receiver:
B4X:
If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
    B4XPages.ShowPage(StartingIntent.Action)
Else
    Dim in As Intent
    in.Initialize(in.ACTION_MAIN, "")
    in.SetComponent(Application.PackageName & "/.main")
    in.PutExtra("page", StartingIntent.Action)
    StartActivity(in)
End If

And in B4XPage_Appear:
B4X:
Private Sub B4XPage_Appear
    Dim NewIntent As Intent = B4XPages.GetNativeParent(Me).GetStartingIntent
    If PreviousIntent.IsInitialized = False Or PreviousIntent <> NewIntent Then
        PreviousIntent = NewIntent
        If NewIntent.HasExtra("page") Then...

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example based on NB6 example is attached.

1. You need the draw on top permission.
2. The interesting code is in the receiver:
B4X:
If B4XPages.IsInitialized And B4XPages.GetManager.IsForeground Then
    B4XPages.ShowPage(StartingIntent.Action)
Else
    Dim in As Intent
    in.Initialize(in.ACTION_MAIN, "")
    in.SetComponent(Application.PackageName & "/.main")
    in.PutExtra("page", StartingIntent.Action)
    StartActivity(in)
End If

And in B4XPage_Appear:
B4X:
Private Sub B4XPage_Appear
    Dim NewIntent As Intent = B4XPages.GetNativeParent(Me).GetStartingIntent
    If PreviousIntent.IsInitialized = False Or PreviousIntent <> NewIntent Then
        PreviousIntent = NewIntent
        If NewIntent.HasExtra("page") Then
            B4XPages.ShowPage(NewIntent.GetExtra("page"))
        End If
    End If
End Sub
 

Attachments

  • NB6.zip
    89.8 KB · Views: 11
Upvote 0
Solution

walterf25

Expert
Licensed User
Longtime User
Thank you, worked like a charm.

Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…