I'm trying to use the example code Notifications permission with targetSdkVersion = 33
I need that when the application is first launched, only the notification permission is set, and the notification itself does not occur.
How can this be done?
In my apps I put this code in then B4XPage_Created sub
B4X:
Dim ph As Phone
If ph.SdkVersion > 32 Then 'Android 13+ (33)
If (kvs.GetDefault("shownotification", True) = True) Then GetPermissionNotification
End If
The GetPermissionNotification shows a Msgbox2Async to the user confirm if will activate or not the notifications and set the "shownotification" to false in the KVS.
Sub GetPermissionNotification
Msgbox2Async("Deseja ativar as notificações?", "", "ATIVAR", "", "Cancelar", Null, True)
Wait For Msgbox_Result (Result As Int)
If Result <> DialogResponse.POSITIVE Then Return
Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean) 'same from the example
kvs.Put("shownotification", False)
End Sub
Can you please tell me if it is possible to read the SetInfo values after they are set in line #7 of the example before the notification is executed?
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
In my apps I put this code in then B4XPage_Created sub
B4X:
Dim ph As Phone
If ph.SdkVersion > 32 Then 'Android 13+ (33)
If (kvs.GetDefault("shownotification", True) = True) Then GetPermissionNotification
End If
out of curiosity, why are you using the Created rather than the Appear sub?
the user can revoke permission in the Android settings after the app was started.
using the Appear you will have a chance to get this, because at the next app foreground event, permissions will be checked
Because I don't want to show this permission every time.
I have an option that save the push notifications, even the notification don't show and user can read then and get an incentive to activate the permission later.