Android Question Is notification runtime permission supported in latest B4A?

DonManfred

Expert
Licensed User
Longtime User
B4A is not ready for TargetSDK 33 as of now.

When it is time for Api 33 B4A will be ready too i guess.
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
B4A is not ready for TargetSDK 33 as of now.
This is Android 13 behavior, no matter what is TargetSDK.
If you target any lower TargetSDK, by default no notification is shown on Android 13, not even notification for foreground services.
We need a way to show notification runtime permission on Android 13 somehow, as Android 13 is released over a month now:

1664975544929.png
 
Upvote 0

Modern_Digital

Member
Licensed User
Longtime User
Hi,

You can check if your app is allowed to send notifications using this function :

B4X:
Private Sub AreNotificationsEnabled As Boolean
   
   Dim nJO As JavaObject
   Dim ctxt As JavaObject
 
   ctxt.InitializeContext
 
   nJO = nJO.InitializeStatic("android.support.v4.app.NotificationManagerCompat").RunMethod("from", Array(ctxt))
 
   'areNotificationsEnabled method Added in API level 24
   Return nJO.RunMethod("areNotificationsEnabled", Null)
 
End Sub

And you can open your app notification settings screen for the user using this function :

B4X:
Private Sub AskForEnableNotifications As ResumableSub

    Try
       
        Dim IN As Intent
       
        'android.settings.APP_NOTIFICATION_SETTINGS Added in API level 26
        IN.Initialize("android.settings.APP_NOTIFICATION_SETTINGS", "")
        IN.Flags = 268435456 'FLAG_ACTIVITY_NEW_TASK
        IN.PutExtra("android.provider.extra.APP_PACKAGE", Application.PackageName)

        StartActivityForResult(IN)
       
        Wait For ion_Event (MethodName As String, Args() As Object)      
               
        Return 1
       
    Catch
       
        Return -1
       
    End Try
       
End Sub

usage :

B4X:
If AreNotificationsEnabled = False Then

   Msgbox2Async("ask user to allow", "Note", "Yes", "", "No", Null, False)
   Wait For Msgbox_Result (Result As Int)  

   If Result = DialogResponse.POSITIVE Then
      Wait For (AskForEnableNotifications) Complete (nAskingStatus As Int)
      If AreNotificationsEnabled = False Then Return
   Else
      Return
   End If

End If

And if your app is running on android API level 31 and higher, you can check the alerts and reminders permission using this function, where 1 means allow :

B4X:
Private Sub CheckScheduleExactAlarmsPermission As Int
   
    Try      
                       
        Dim oConText As JavaObject
        Dim oAlarmManager As JavaObject
        Dim IsScheduleExactAlarmsAllawed As Boolean = False
       
        oConText.InitializeContext
       
        oAlarmManager = oConText.RunMethod("getSystemService", Array("alarm"))
       
        If oAlarmManager.IsInitialized Then
         
           IsScheduleExactAlarmsAllawed = oAlarmManager.RunMethod("canScheduleExactAlarms",Null).As(Boolean)
         
           If IsScheduleExactAlarmsAllawed = True Then
              Return 1
           Else
                 Return 0
           End If
         
        Else
         
           Return -2
             
        End If
                 
        Return -3
       
    Catch

        'Log(LastException)
               
        Return -1
       
    End Try      
   
End Sub

And you can open the alerts and reminders settings screen for the user using this function :

B4X:
Private Sub AskForScheduleExactAlarmsPermission As ResumableSub
   
    Try      

        Dim IN As Intent
       
        IN.Initialize("android.settings.REQUEST_SCHEDULE_EXACT_ALARM", "package:" & Application.PackageName)

        StartActivityForResult(IN)
       
        Wait For ion_Event (MethodName As String, Args() As Object)
       
        'Args(0) = 0 'User cancel
        'Args(0) = -1 'User allow
'        If Args.Length > 0 Then          
'           Return Args(0).As(Int)
'        Else
'           Return -3
'        End If
       
        Return 1
       
    Catch

        'Log(LastException)
               
        Return -1
       
    End Try      
   
End Sub

usage :

B4X:
Dim uPhone As Phone

If uPhone.SdkVersion >= 31 And CheckScheduleExactAlarmsPermission <> 1 Then

   Msgbox2Async("ask user to allow", "Note", "Yes", "", "No", Null, False)
   Wait For Msgbox_Result (Result As Int)  

   If Result = DialogResponse.POSITIVE Then            
      Wait For (AskForScheduleExactAlarmsPermission) Complete (rAskingStatus As Int)              
      If CheckScheduleExactAlarmsPermission <> 1 Then Return            
   Else              
      Return                            
   End If

End If

And to make the previous codes above works :

Add these two lines in manifest file :

B4X:
AddPermission(android.permission.SCHEDULE_EXACT_ALARM)
AddPermission(android.permission.POST_NOTIFICATIONS)

define this variable in the Globals section :

B4X:
Sub Globals
    Private ion As Object
End Sub

And add this line :

B4X:
#AdditionalJar: com.android.support:support-v4

And these two functions ;

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

Private Sub GetBA As Object
   Dim jo As JavaObject
   Dim cls As String = Me
   cls = cls.SubString("class ".Length)
   jo.InitializeStatic(cls)
   Return jo.GetField("processBA")
End Sub

Note: Tested on Android studio emulator with API level 31, 32, 33 and they all work without any problems.

Hope this is useful to you.
 
Last edited:
Upvote 0
Top