Android Question Displaying RuntimePermission dialog the second time

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the following code to check if the user has enabled Notifications on there device.

It works and displays a popup message asking to enable the notifications permission.

B4X:
Sub CheckPushNoticationsEnabled
    
    Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
    Log("HasPermission = " & HasPermission)
    If HasPermission = False Then
        Log("no permission")
        
        Else
        Log("got permission")
        
    End If
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

If the user taps 'Don't allow' I am going to make it display a icon in my app to say they don't have the notifications enabled.

If the user taps on 'Don't allow' by mistake how can I prompt the user again?

When I run the above code again, it just logs it doesn't have it enabled and doesn't display the dialog the second time.

Anyone know how to show the dialog again the second time to allow the user to enable the permission, other than opening the settings app and enabling it from the settings app ?
 
Top