Android Question Notifications in B4XPages

Sergey_New

Well-Known Member
Licensed User
Longtime User
Please advise how to allow notifications when opening the B4XMainPage page?
I use minSdkVersion="9" android:targetSdkVersion="29".
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Erel, thank you! Notifications in the app are enabled.
But the next time you launch the app, a beep sounds. The app does not use the beep.
B4X:
n.SetInfo("This is the title", "and this is the body.", Main)'Change Main to "" if this code is in the main module.
Replacing Main with "" is this for B4A, or can it be used in B4XPages?

P.S.
I figured out the sound - it's a signal about an incoming notification.
I just need to allow notifications for my application. Sending notifications, if necessary, must be done via a separate command.
 
Last edited:
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Managed to set notification permission without launching the notification.
Placed this code in the Main module:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
    checkNotifications
End Sub

Sub checkNotifications
    If AreNotificationsEnabled = False Then
        Wait For (AskForEnableNotifications) Complete (nAskingStatus As Int)
        If AreNotificationsEnabled = False Then
            Return
        End If
    Else
        Return
    End If
End Sub

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))
    Return nJO.RunMethod("areNotificationsEnabled", Null)
End Sub

Private Sub AskForEnableNotifications As ResumableSub
    Try
        Dim IN As Intent
        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

Private Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    Dim ion As Object
    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
 
Upvote 0
Top