Android Question NB6 for B4A

Sergey_New

Well-Known Member
Licensed User
Longtime User
After reading the entire forum, I did not find a suitable example.
How to properly initialize an intent in code:
Main:
Private Sub Activity_Resume
    Dim in As Intent = B4XPages.GetNativeParent(Me).GetStartingIntent
    If in.IsInitialized And in <> OldIntent Then
        OldIntent = in
        If in.HasExtra("Notification_Tag") Then
            Log("Activity started from notification. Tag: " & in.GetExtra("Notification_Tag"))
        End If
    End If
End Sub
 
Solution
This line is for B4XPages:
B4X:
    Dim in As Intent = B4XPages.GetNativeParent(Me).GetStartingIntent
If you are mistakenly not using B4XPages:
B4X:
    Dim in As Intent = Activity.GetStartingIntent

Sergey_New

Well-Known Member
Licensed User
Longtime User
This line is for B4XPages:
Thank you, this is what I needed!
I made it so that notification permission is allowed when installing the application, and it is launched when pressing the button.
I hope this code is correct:
Main:
Sub Process_Globals
    Private xui As XUI
End Sub

Sub Globals
    Private smiley As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    smiley = LoadBitmapResize(File.DirAssets, "smiley.png", 24dip, 24dip, False)
End Sub

Private Sub CheckAndRequestNotificationPermission As ResumableSub
    Dim p As Phone
    If p.SdkVersion < 33 Then Return True
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim targetSdkVersion As Int = ctxt.RunMethodJO("getApplicationInfo", Null).GetField("targetSdkVersion")
    If targetSdkVersion < 33 Then Return True
    Dim NotificationsManager As JavaObject = ctxt.RunMethod("getSystemService", Array("notification"))
    Dim NotificationsEnabled As Boolean = NotificationsManager.RunMethod("areNotificationsEnabled", Null)
    If NotificationsEnabled Then Return True
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_POST_NOTIFICATIONS)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean) 'change to Activity_PermissionResult if non-B4XPages.
    Log(Permission & ": " & Result)
    Return Result
End Sub

Sub HighPriority_Notification
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "HIGH").SmallIcon(smiley)
    n.Build("Important!!!", "Content", "tag", Me).Notify(1)
End Sub

Private Sub Button1_Click
    HighPriority_Notification
End Sub

Private Sub Activity_Resume
    Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
    If HasPermission Then
'        CallSub(Me, HighPriority_Notification)
    Else
        ToastMessageShow("No permission to show notification", True)
    End If
    Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized Then
        If in.HasExtra("Notification_Tag") Then
            Log("Activity started from notification. Tag: " & in.GetExtra("Notification_Tag"))
        End If
    End If
End Sub
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I hope this code is correct:
Unfortunately, when you refuse to allow notifications, the message window does not close and when you refuse again, an incomprehensible cycle occurs. What needs to be fixed?
An example is attached.
 

Attachments

  • NB6_B4A.zip
    13.2 KB · Views: 9
Upvote 0
Top