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,
- No need to add google-services.json
- Multiple firebase project work with single code.
- No need to add FirebaseInstanceIdService in Manifest.