Hi,
You can check if your app is allowed to send notifications using this function :
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 :
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 :
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 :
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 :
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 :
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 :
AddPermission(android.permission.SCHEDULE_EXACT_ALARM)
AddPermission(android.permission.POST_NOTIFICATIONS)
define this variable in the Globals section :
Sub Globals
Private ion As Object
End Sub
And add this line :
#AdditionalJar: com.android.support:support-v4
And these two functions ;
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.