Android Code Snippet Notifications permission with targetSdkVersion = 33

There is a new "dangerous" (runtime) permission On Android 13+ devices required for showing notifications. When targetSdkVersion < 33, the OS will show the permission dialog automatically before the notification is displayed.
Once we switch to targetSdkVersion=33 we are responsible for requesting it ourselves. This has the advantage that we can control the exact point where it will be requested.

This code handles this:
Depends on JavaObject and Phone libraries.
B4X:
'Make sure that targetSdkVersion >= 33
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 B4XPage_PermissionResult (Permission As String, Result As Boolean) 'change to Activity_PermissionResult if non-B4XPages.
    Log(Permission & ": " & Result)
    Return Result
End Sub

Usage example:
B4X:
Private Sub Button1_Click
    Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
    If HasPermission Then
        Dim n As Notification
        n.Initialize
        n.Icon = "icon"
        n.SetInfo("This is the title", "and this is the body.", Main) 'Change Main to "" if this code is in the main module.
        n.Notify(1)
    Else
        Log("no permission")
        ToastMessageShow("no permission", True)
    End If
End Sub
 
Last edited:

Alessandro71

Well-Known Member
Licensed User
Longtime User
Can CheckAndRequestNotificationPermission be in a service module, since notification are built there?
 

DonManfred

Expert
Licensed User
Longtime User
Can CheckAndRequestNotificationPermission be in a service module, since notification are built there?
probably not as you need to have an active activity to request permissions.

PS: You should have started a new thread for any question. As always...
 

bobbruns

Member
Licensed User
Longtime User
When I put GPS into the background (service) some devices showed a notification. Do I need to request permission? I switched to sdk target = 33 and there is no permission required under "list permissions."
 
Last edited:

marcick

Well-Known Member
Licensed User
Longtime User
No need to update the Manifest ?

B4X:
<manifest ...>
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
    <application ...>
        ...
    </application>
</manifest>
 
Top