9
votes

I have 2 projects in Firebase: nl.companyname and nl.companyname.acc:

enter image description here

This is my build.gradle:

flavorDimensions "type"
productFlavors {
    acceptance {
        dimension="type"
        applicationIdSuffix ".acc"
        versionNameSuffix "-acc"
    }
    production {
        dimension="type"
        applicationIdSuffix ""
        versionNameSuffix ""
    }
}

The download google-services.json is in directory: app/google-services.json

Android Studio is logged in to the Google account and synchronized:

enter image description here

The message shows up as successfully sent: enter image description here

Problem description:

  • When sending a message on nl.companyname, it works.

  • When sending a message and targeting the device's Token ID, it works.

  • But the nl.companyname.acc doesn't work.

Steps tried:

  • I've deleted the .acc App in Firebase and re-added it (and downloaded the new json file).

enter image description here

Any help is greatly appreciated.

3
You need to change the approach to adding google-services.json file in app folder.there is alternative solutions which you need to use. - Kintan Patel

3 Answers

8
votes

In the Firebase Document, it supports multiple flavor based project.

You can have multiple google-services.json files for different build variants) by placing google-services.json files in dedicated directories named for each variant under the app module root. For example, if you have "development" and "release" build flavors, your configuration could be organized like this:

app/
    google-services.json
    src/development/google-services.json
    src/release/google-services.json
    ...

You can find full instruction at here.

0
votes

Are you sure that your google-services.json are like this?

{
  "project_info": {
    ...
  },
  "client": [
    {
      "client_info": {
        ...
        "android_client_info": {
          "package_name": "nl.companyname"
        }
      },
      ...
    },
    {
      "client_info": {
        ...
        "android_client_info": {
          "package_name": "nl.companyname.acc"
        }
      },
      ...
    }
  ],
  ...
}

On your app/gralde.build try something like this

buildTypes {
    acceptance {
        applicationIdSuffix '.acc'
        ...
    }
    release {
        ...
    }
}
0
votes

Remove google-services.json from build.gradle

Create Application class

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId("0")
                .build();
        FirebaseApp.initializeApp(this, options);
    }

}

Register this class in Manifest

 <application
        android:name=".extra.MyApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">

On your Main Activity you need to call below method which generates FCM Token,(Which is similar like FirebaseInstanceIdService)

new GetFCMToken().execute();

 private class GetFCMToken extends AsyncTask<String,Integer,String>{

        @Override
        protected String doInBackground(String... params) {
            //you can update this id based on your flavour
        String senderId = "1223";//put your FireBase project Sender Id


            String sToken = "";
            try {
                // Check for current token

                // Resets Instance ID and revokes all tokens.
                FirebaseInstanceId.getInstance().deleteInstanceId();

                // Clear current saved token
                // Check for success of empty token


                // Now manually call onTokenRefresh()
                Log.d("TAG", "Getting new token");
                sToken = FirebaseInstanceId.getInstance().getToken(senderId, "FCM");
                Log.d("TAG", "s" + sToken);

            } catch (IOException e) {
                e.printStackTrace();
                Log.e("e", "e", e);
            }
            return sToken;
        }

        @Override
        protected void onPostExecute(String fcmToken) {
            super.onPostExecute(fcmToken);
            //Use this token to send notification
            Log.e("FCM_TOKEN",fcmToken);
           //Send Token server


        }

    }

You can find SenderId from firebase cosole -->Project Setting --> Cloud Messaging. And Make sure you have added your package name in Fcm Console.

FirebaseMessegingReceiver.class work as it is.

Main benefit of this code is that,

  1. No need to add google-services.json
  2. Multiple firebase project work with single code.
  3. No need to add FirebaseInstanceIdService in Manifest.