Skip to content

Applying Firebase configuration in MobileMessaging SDK

Olga Koroleva edited this page Nov 9, 2022 · 1 revision

MobileMessaging SDK supports following ways of applying Firebase configuration:

  1. [Preferable] Add a Firebase configuration file (google-services.json) as described in Firebase documentation

Notice:

If you want to switch between types of configuration clean your project first, otherwise you can receive "Duplicate resources" errors.

  1. Add key/values from google-services.json to resource strings (strings.xml). Documentation of the Google Services Gradle Plugin gives the details of how to get these values from google-services.json file.
<resources>
    ...
    <! -- Required -->
    <string name="google_app_id" translatable="false">1:1035469437089:android:73a4fb8297b2cd4f</string>
    <string name="google_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>
    <string name="project_id" translatable="false">mydemoapp</string>

    <! -- Optional -->
    <string name="gcm_defaultSenderId" translatable="false">1035469437089</string>
    <string name="default_web_client_id" translatable="false">337894902146-e4uksm38sne0bqrj6uvkbo4oiu4hvigl.apps.googleusercontent.com</string>
    <string name="ga_trackingId" translatable="false">UA-65557217-3</string>
    <string name="firebase_database_url" translatable="false">https://example-url.firebaseio.com</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8</string>

</resources>

Notice:

apply plugin: 'com.google.gms.google-services' line should be removed from the build.gradle file, if it was added

  1. Initialize FirebaseOptions object and provide it to MobileMessaging.Builder#withFirebaseOptions(FirebaseOptions) method. Documentation of the Google Services Gradle Plugin gives the details of how to get these values from google-services.json file.
        val firebaseOptions = FirebaseOptions.Builder() 
                //required parameters
                .setApiKey("AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8")
                .setApplicationId("1:1035469437089:android:73a4fb8297b2cd4f")
                .setProjectId("mydemoapp")
                .build()
        MobileMessaging.Builder(getApplication())
                .withFirebaseOptions(firebaseOptions)
                .build()
expand to see Java code

        FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
                //required parameters
                .setApiKey("AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8")
                .setApplicationId("1:1035469437089:android:73a4fb8297b2cd4f")
                .setProjectId("mydemoapp")
                .build();

        new MobileMessaging.Builder(getApplication())
                .withFirebaseOptions(firebaseOptions)
                .build();

Notice:

apply plugin: 'com.google.gms.google-services' line should be removed from the build.gradle file, if it was added

Clone this wiki locally