Skip to content

Applying Firebase configuration in MobileMessaging SDK

Davor Komušanac edited this page Dec 5, 2022 · 1 revision

MobileMessaging SDK supports following ways of applying Firebase configuration:

  1. [Preferable] Using Firebase configuration file (google-services.json)
  2. Adding key/values from google-services.json to resource strings
  3. Providing FirebaseOptions object to mobileMessaging.init method

Notice:

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

[Preferable] Using Firebase configuration file (google-services.json)

  1. Add 'com.google.gms:google-services' to android/build.gradle file
         buildscript {
            ...
            dependencies {
                ...
               //Google Services gradle plugin
               classpath 'com.google.gms:google-services:4.3.10'
            }
         }
  2. Add apply plugin: 'com.google.gms.google-services' at the end of your android/app/build.gradle in order to apply Google Services Gradle Plugin
  3. Setup Firebase for your project if it's not yet and add Firebase configuration file (google-services.json) to the app as described in Firebase documentation. Usually it needs to be added into android/app folder.

Adding key/values from google-services.json to resource strings

Add key/values from google-services.json to resource strings (app/src/main/res/values/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

Providing FirebaseOptions object to mobileMessaging.init method

Documentation of the Google Services Gradle Plugin gives the details of how to get these values from google-services.json file.

        var firebaseOptions = {
            //required parameters
            apiKey:"AIzbSyCILMsOuUKwN3qhtxrPq7FFemDJUAXTyZ8",
            applicationId:"1:1035469437089:android:73a4fb8297b2cd4f",
            projectId:"mydemoapp",
        };
        mobileMessaging.init(
        {
            applicationCode: '<your app code>',
            ios: {
                notificationTypes: ['alert', 'badge', 'sound'],
            },
            android: {
                firebaseOptions: firebaseOptions,
            }
         },
         () => {
                console.log('MobileMessaging started');
         },
         error => {
                console.log('MobileMessaging error: ', error);
         },
      );

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