[SWIFT] Firebase Remote Config installation procedure

What is Firebase Remote Config?

By registering the value of Key: Value on the console side of Firebase Remote Config, you can update the value in the cloud.

merit

When you change the behavior or appearance of your app, you can change it by simply modifying the settings on the Firebase Remote Config console without distributing the app update. Basically, the same key-value can be distributed and reflected in real time to all users, and it can be used in the following cases.

--You can also change the Value by specifying the conditions (app version, OS, specified% of users, etc.). --Can also be used for A / B testing.

It seems that it is often used to manage the version of the application with Firebase Remote Config and prompt for forced update.

Demerit

Not suitable for immediate reflection

Basically fetching (getting a value from the cloud) is done only once every 12 hours. If fetched once, the cached value will be used until the cache remains and fetched again. Therefore, using Firebase Remote Config is not suitable for getting values that require immediate change.

Installation procedure

Settings in the Firebase console

1. Open the Firebase console and select Remote Config from the menu.

2. Setting conditions

Here, you can set the conditions for getting each value.

About the conditions

--Device region / country --Current time and time obtained on the device --App version --Build number

You can set it with.

Here, we will determine whether the device is Japan or not. image.png

It is also possible to set conditions such as.

3. Parameter key settings

Based on the conditions decided above

-Device region/Country=For Japan: Yahoo URL
-Device region/Country=Outside Japan: Google URL

I will set to get. This allows you to change the destination URL depending on the country of the device.

Swift side

1. Create a Remote Config object


remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()

//Fetch interval setting (development environment only)
settings.minimumFetchInterval = 0

remoteConfig.configSettings = settings

Remote Config is basically fetched once and not fetched again within the cache time (12 hours by default). Therefore, it is necessary to set minimumFetchInterval as above in the development environment. This allows the development environment to fetch regardless of the fetch interval. However, be careful not to write this code in production.

Use this setting for development purposes only and not for apps that run in production. If you just test your app with a small development team of 10 people, it's unlikely that you'll reach your hourly service-side quota. However, if you set the minimum fetch interval to a very small value and push your app to thousands of test users, your app is likely to reach this quota.

https://firebase.google.com/docs/remote-config/use-config-ios?hl=ja

2. Setting the default value

Set here the default value to be used when the value is not set on the Firebase Remote Config side or it cannot be obtained offline. Create a plist file to define a set of parameter names and default values.

//RemoteConfigDefaults is the filename of the plist file
remoteConfig.setDefaults(fromPlist: "RemoteConfigDefaults")

3. Read value


remoteConfig.fetchAndActivate { (status, error) -> Void in
            if status == .successFetchedFromRemote || status == .successUsingPreFetchedData {
                //Describe the processing when fetch is completed
                print("Config fetched!")
                //The value obtained this time is in json format, so it needs to be decoded.
                let jsonString = remoteConfig["parameter"].stringValue
                let jsonData = jsonString?.data(using: .utf8)
                do {
                    //The value obtained from Remote Config is stored in getUrl.
                    var urlDictionary = try JSONSerialization.jsonObject(with: jsonData!, options: [])
                    guard let dictionary = urlDictionary as? [String: Any],
                    let getUrl = dictionary["url"] as? [String: Any] else { return }
            } else {
                //Describe the processing when fetch fails
                print("Config not fetched")
            }
        }
}

The process of fetching the value and activating the fetched value is performed at the same time by fetchAndActivate. By implementing in this way, you can get the value from Firebase Remote Config and use it in the application.

Recommended Posts

Firebase Remote Config installation procedure
eclipse installation procedure
Java (eclipse) installation procedure
Doorkeeper gem installation procedure
Rails, RSpec installation procedure
Amazon Corretto for Windows installation procedure
Windows version of OpenJDK installation procedure