Android Question Checkbox value not stored

Isac

Active Member
Licensed User
Longtime User
What do I want to do?

I would like to disable the arrival of messages when I press on the CheckBox (true) and enable the arrival of messages when the CheckBox is (false).

what's the problem?

Currently when I press on the checkbox it becomes true,
and in the FirebaseMessaging module it is true
but if disabled, the checkbox (false) in the FirebaseMessaging module remains true.


Main

B4X:
#Region  Project Attributes
    #ApplicationLabel: Notifica
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#BridgeLogger:true
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim OldIntent As Intent
    Dim Label3 As Label
    Dim Label4 As Label
    Public CheckBox1 As CheckBox

    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Layout1")
    FirebaseMessaging.kvs.Initialize(File.DirDefaultExternal,"datastore")
    If FirebaseMessaging.kvs.ContainsKey("CheckBox1") Then CheckBox1.Checked = FirebaseMessaging.kvs.Get("CheckBox1")

    'Do not forget to load the layout file created with the visual designer. For example:

End Sub
    



Sub Activity_Resume
    Dim in As Intent = Activity.GetStartingIntent
    If in.IsInitialized And in <> OldIntent Then
        OldIntent = in
        Dim intentExtra As String
        If in.HasExtra("Notification_Tag") Then
            intentExtra = in.GetExtra("Notification_Tag")
            Log("XTRA="&intentExtra)
            Log(in)
            Log(in.ExtrasToString)
            Dim jp As JSONParser
            jp.Initialize(intentExtra)
            Dim root As Map = jp.NextObject
            Label4.Text = root.Get("title1")
            Label3.Text = root.Get("body2")
            
        End If
    End If
End Sub

'Sub SetText (Title As String)
'    Label3.Text = Title
'End Sub

Sub Activity_Pause (UserClosed As Boolean)
    

    
End Sub



Sub CheckBox1_CheckedChange(Checked As Boolean)
    FirebaseMessaging.kvs.Put("CheckBox1",Checked)
    Log("Checked="&Checked)
End Sub






FirebaseMessaging


B4X:
#Region  Service Attributes
    #StartAtBoot: False
   
#End Region

Sub Process_Globals
    Private fm As FirebaseMessaging
    Dim mano As Bitmap
'    Dim value As String
'    Public notifications As Boolean
    Public kvs As KeyValueStore

    End Sub




Sub Service_Create
    fm.Initialize("fm")
    kvs.Initialize(File.DirDefaultExternal,"datastore")

End Sub

Public Sub SubscribeToTopics
    fm.SubscribeToTopic("all") 'you can subscribe to more topics

End Sub

Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then fm.HandleIntent(StartingIntent)
    Sleep(0)
    Service.StopAutomaticForeground 'remove if not using B4A v8+.
End Sub


Sub fm_MessageArrived (Message As RemoteMessage)
    Service_Create
    Log("Message arrived")
    Log($"Message data: ID ${Message.MessageId}, Data ${Message.GetData}"$)
    Log("Message="& Message)
    Dim map As Map :map.Initialize
    map = CreateMap("body2":Message.GetData.Get("body"),"title1":Message.GetData.Get("title"))
    For Each key As String In map.Keys  
    Dim value As String = map.Get(key)
    Next
    Log($"${key}:${value}"$)

    If  kvs.ContainsKey("CheckBox1") Then
        Log(kvs.Get("CheckBox1"))
        Log("modulo_Firebase_:true")
       
        'message does not arrive
    Else
        Log("modulo_Firebase_:false")
   
    Dim jgen As JSONGenerator
    jgen.Initialize(map)
    Dim n As NB6
    mano = LoadBitmapResize(File.DirAssets, "mano.png", 24dip, 24dip, False)
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(mano)
    n.Build(Message.GetData.Get("title"), Message.GetData.Get("body"), jgen.ToString, Main).Notify(4)
    End If
End Sub

   



Sub Service_Destroy

End Sub
 

npsonic

Active Member
Licensed User
There is multiple errors in your example. You should declare and initialize KeyValueStore in Starter service.
You shouldn't call Service_Create from method MessageArrived. FirebaseMessaging is started when you receive message.

You can prevent new messages with something like this.

B4X:
Sub fm_MessageArrived (Message As RemoteMessage)
    If Starter.kvs.ContainsKey("CheckBox1") Then
        If Not(Starter.kvs.Get("CheckBox1")) Then Return
    End If

    'Your other stuff

End Sub
 
Last edited:
Upvote 0
Top