B4A Library Firebase RemoteConfig

This library allows you to use Firebase RemoteConfig to your app.

Requirements:
- B4A 6+
- To use this library you need to Setup your app to use Firebase-Services first.

Go to the Firebase console and setup remoteconfig




RemoteConfig
Author:
DonManfred (wrapper)
Version: 2.50
  • RemoteConfig
    Events:
    • onFetchComplete (success As Boolean)
  • Fields:
    • DEFAULT_VALUE_FOR_BOOLEAN As Boolean
    • DEFAULT_VALUE_FOR_DOUBLE As Double
    • DEFAULT_VALUE_FOR_LONG As Long
    • DEFAULT_VALUE_FOR_STRING As String
    • LAST_FETCH_STATUS_FAILURE As Int
    • LAST_FETCH_STATUS_NO_FETCH_YET As Int
    • LAST_FETCH_STATUS_SUCCESS As Int
    • LAST_FETCH_STATUS_THROTTLED As Int
    • VALUE_SOURCE_DEFAULT As Int
    • VALUE_SOURCE_REMOTE As Int
    • VALUE_SOURCE_STATIC As Int
  • Methods:
    • Defaults (defaults As Map)
    • Initialize (EventName As String)
    • IsInitialized As Boolean
    • activateFetched As Boolean
      Fetches parameter values for your app. Parameter values may be from the
      Default Config (local cache), or from the Remote Config Server,
      depending on how much time has elapsed since parameter values were last
      fetched from the Remote Config server. This method uses the default
      cache expiration of 12 hours.
      Return type: @return:
    • fetch (cacheExpiration As Int)
    • getBoolean (key As String) As Boolean
      Gets the value corresponding to the specified key,
      as a boolean.
    • getBoolean2 (key As String, namespace As String) As Boolean
      Gets the value corresponding to the specified key,
      as a boolean, in the specified namespace.
    • getByteArray (key As String) As Byte[]
      Gets the value corresponding to the specified
      key as a byte array.
      Returns Value as a byte array if a value
      corresponding to the look up key was present;
      default (if set) or static default value otherwise.
    • getByteArray2 (key As String, namespace As String) As Byte[]
      Gets the value corresponding to the specified
      key, in the specified namespace, as a byte array.
      Returns Value as a byte array if a value
      corresponding to the look up key was present;
      default (if set) or static default value otherwise.
    • getDouble (key As String) As Double
      Gets the value corresponding to the specified
      key, in the specified namespace, as a double.
      Returns Value as a double if a value corresponding
      to the look up key was present and could be
      converted to a double; default (if set) or
      static default value otherwise.
    • getDouble2 (key As String, namespace As String) As Double
      Gets the value corresponding to the specified
      key as a double.
      Returns Value as a double if a value corresponding
      to the look up key was present and could be
      converted to a double; default (if set) or
      static default value otherwise.
    • getLong (key As String) As Long
      Gets the value corresponding to the
      specified key, as a long.

      returns Value as a long if a value corresponding
      to the look up key was present and could be
      converted to a long; default (if set) or
      static default value otherwise.
    • getLong2 (key As String, namespace As String) As Long
      Gets the value corresponding to the specified
      key, in the specified namespace, as a long.
    • getString (key As String) As String
      Gets the value corresponding to
      the specified key, as a string.

      returns alue as a string if a value corresponding
      to the look up key was present and could be converted
      to a string; default (if set) or
      static default value otherwise.
    • getString2 (key As String, namespace As String) As String
      Gets value as a string corresponding
      to the specified key in the specified namespace.
      returns alue as a string if a value corresponding
      to the look up key was present and could be converted
      to a string; default (if set) or
      static default value otherwise.
    • getValue (key As String) As FirebaseRemoteConfigValue
      Gets the FirebaseRemoteConfigValue
      corresponding to the specified key.
    • getValue2 (key As String, namespace As String) As FirebaseRemoteConfigValue
    • setDefaults2 (defaults As Map, namespace As String)
  • Properties:
    • ConfigSettings As FirebaseRemoteConfigSettings [write only]
      Changes the settings for the FirebaseRemoteConfig
      object's operations, such as turning the developer mode on.
    • Info As FirebaseRemoteConfigInfo [read only]
      Gets the current state of the
      FirebaseRemoteConfig singleton object.
      returns A FirebaseRemoteConfigInfo wrapping the current state.
  • RemoteConfigValue
    Methods:
    • Initialize (cfg As FirebaseRemoteConfigValue)
    • IsInitialized As Boolean
    • asBoolean As Boolean
    • asByteArray As Byte[]
    • asDouble As Double
    • asLong As Long
    • asString As String
  • Properties:
    • Source As Int [read only]
      VALUE_SOURCE_REMOTE if the value was retrieved
      from the server, VALUE_SOURCE_DEFAULT if the
      value was set as a default, or VALUE_SOURCE_STATIC
      if no value was found and a static default
      value was returned instead.


B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim cfg As RemoteConfig
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    cfg.Initialize("Config")
    cfg.Defaults(CreateMap("Month": 1, "loading_phrase": "Hallo world!"))
    Log("loading_phrase="&cfg.getString("loading_phrase"))
    Log("Fetching")
    cfg.fetch(86400)
End Sub

Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Config_onFetchComplete(success As Boolean)
    Log($"Config_onFetchComplete(${success})"$)
    If success Then
        Log("Activate fetched values")
        cfg.activateFetched
        Log("loading_phrase="&cfg.getString("loading_phrase"))
    End If

End Sub



SETUP:
- Use V2.5+ of the Library.
- Follow the Firebaseintegration Tutorial

Replace in the Manifesteditor the line
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.Firebase)

with

B4X:
CreateResourceFromFile(Macro, FirebaseRemoteConfig.Firebase)

NOTES:
- If you ran into an Error
protobuf-lite Maven artifact not found
then please copy the file com.google.protobuf-protobuf-lite.jar to your additional library folder
 

Attachments

  • FirebaseRemoteConfigV2.01.zip
    7.7 KB · Views: 893
  • RemoteConfigEx.zip
    8.2 KB · Views: 891
  • src.zip
    3.6 KB · Views: 819
  • com.google.protobuf-protobuf-lite.jar
    240.7 KB · Views: 859
  • FirebaseRemoteConfigV2.52.zip
    8.6 KB · Views: 871
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Based on the Eclipse onlinehelp the Method is deprecated.


void setDefaults(Map<String, Object> defaults)
This method was deprecated. Use setDefaultsAsync(int) instead.

Problem is that it now only allows to set a resourceid which must be a XML (i guess) in the Resourcefolder.

Unsure how to fix it. I guess we need to define the defaults in a xml now.

Edit to add:


looks like we need to use such a xml. But not sure how to get it´s ID!?
 
Last edited:

ivan.tellez

Active Member
Licensed User
Longtime User
Based on the Eclipse onlinehelp the Method is deprecated.

As it says there, (Use setDefaultsAsync(int) instead. )

Problem is that it now only allows to set a resourceid which must be a XML (i guess) in the Resourcefolder.

That method has another option with a Map:

Java:
setDefaultsAsync(int resourceId)
Sets default configs using an XML resource.
  
setDefaultsAsync(Map<String, Object> defaults)
Asynchronously sets default configs using the given Map.

Source:
 

SMOOTSARA

Active Member
Licensed User
Longtime User
Hi DonManfred ?

What is this error?

b4x ver 10.2 beta
lib ver 2.52

Thanks

B4X:
*** Service (starter) Create ***
remote_host_initialize (B4A line: 31)
cfg.Initialize("Config")
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.remoteconfig.FirebaseRemoteConfig com.google.firebase.remoteconfig.RemoteConfigComponent.getDefault()' on a null object reference
    at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(FirebaseRemoteConfig.java:87)
    at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(FirebaseRemoteConfig.java:81)
    at de.donmanfred.FirebaseRemoteConfigwrapper.Initialize(FirebaseRemoteConfigwrapper.java:36)
    at xxxx.xxx.com.remote_host._initialize(remote_host.java:696)
    at xxxx.xxx.com.starter._service_create(starter.java:404)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at xxxx.xxx.com.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3953)
    at android.app.ActivityThread.access$1500(ActivityThread.java:219)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
java.lang.RuntimeException: Unable to create service xxxx.xxx.com.starter: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.remoteconfig.FirebaseRemoteConfig com.google.firebase.remoteconfig.RemoteConfigComponent.getDefault()' on a null object reference
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3965)
    at android.app.ActivityThread.access$1500(ActivityThread.java:219)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.remoteconfig.FirebaseRemoteConfig com.google.firebase.remoteconfig.RemoteConfigComponent.getDefault()' on a null object reference
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:250)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at xxxx.xxx.com.starter.onCreate(starter.java:56)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3953)
    ... 8 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.remoteconfig.FirebaseRemoteConfig com.google.firebase.remoteconfig.RemoteConfigComponent.getDefault()' on a null object reference
    at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(FirebaseRemoteConfig.java:87)
    at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(FirebaseRemoteConfig.java:81)
    at de.donmanfred.FirebaseRemoteConfigwrapper.Initialize(FirebaseRemoteConfigwrapper.java:36)
    at xxxx.xxx.com.remote_host._initialize(remote_host.java:696)
    at xxxx.xxx.com.starter._service_create(starter.java:404)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    ... 11 more
Initialize OneSignal
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…