2
    public class LoginActivity extends AppCompatActivity {

    private static final int RC_SIGN_IN = 1;

    @BindView(R.id.activity_login_google_button)
    SignInButton googleButton;
    @BindView(R.id.activity_login_progress)
    ProgressBar progress;

    private GoogleApiClient mGoogleApiClient;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private FirebaseAuth mAuth;

    @State
    boolean isLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        Icepick.restoreInstanceState(this, savedInstanceState);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestIdToken(getString(R.string.default_web_client_id))
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        mAuth = FirebaseAuth.getInstance();

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d("@@@@", "onAuthStateChanged:signed_in:" + user.getUid());
                    Intent home = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(home);
                    finish();
                } else {
                    // User is signed out
                    Log.d("@@@@", "onAuthStateChanged:signed_out");
                }
            }
        };

        displayLoadingState();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mAuthListener != null) {
            mAuth.addAuthStateListener(mAuthListener);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            hideProgress();
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleGoogleSignInResult(result);
        }
    }

    private void hideProgress() {
        isLoading = false;
        displayLoadingState();
    }

    private void displayLoadingState() {
        progress.setVisibility(isLoading ? VISIBLE : GONE);
        googleButton.setVisibility(!isLoading ? VISIBLE : GONE);
    }

    private void showProgress() {
        isLoading = true;
        displayLoadingState();
    }

    private void handleGoogleSignInResult(GoogleSignInResult result) {
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        } else {
            Toast.makeText(this, R.string.error_google_sign_in, Toast.LENGTH_SHORT).show();
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d("@@@@", "firebaseAuthWithGoogle:" + acct.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("@@@@", "firebaseAuthWithGoogleComplete:" + task.isSuccessful());

                        Needle.onBackgroundThread().execute(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    FirebaseInstanceId.getInstance().deleteInstanceId();
                                } catch (IOException e) {
                                    Log.d("@@@@", "deleteInstanceIdCatch: " + e.getMessage());
                                }
                            }
                        });

                        if (!task.isSuccessful()) {
                            Log.w("@@@@", "firebaseAuthWithGoogleFailed", task.getException());
                            Toast.makeText(LoginActivity.this, R.string.error_google_sign_in, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    @OnClick(R.id.activity_login_google_button)
    public void attemptGoogleSignIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
        showProgress();
    }
}    

this is the login activity code in which I'm getting an error that cannot find symbol class FirebaseInstanceIdService I have attached an image for reference

this is for my final year project which is a chatting based application because of this process, I cannot able to generate APK but Gradle sync is successful.

Niraj
  • 903
  • 8
  • 23
Ajith
  • 25
  • 2
  • 9

1 Answers1

7

Developers are stuck with the problem of deprecated FirebaseInstanceIdService, So now what to do?

FirebaseInstanceIdService

This class was deprecated. In favour of overriding onNewToken in FirebaseMessagingService. Once that has been implemented, this service can be safely removed. That means no need to use FirebaseInstanceIdService service to get an FCM token. You can safely remove FirebaseInstanceIdService service

Now we need to @Override onNewToken() get Token in "FirebaseMessagingService".

Sample Code:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

        @Override
        public void onNewToken(String s) {
            super.onNewToken(s);
            Log.e("NEW_TOKEN",s);
        }

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
        }
    } 

In AndroidManifest.xml

    <service
            android:name=".MyFirebaseMessagingService"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
    </service>

Get Token in your Activity : .getToken(); is also deprecated if you need to get token in your activity then use as following:

    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {
         @Override
         public void onSuccess(InstanceIdResult instanceIdResult) {
               String newToken = instanceIdResult.getToken();
               Log.e("newToken",newToken);

         }
     });

I hope you will get your solution here.

Niraj
  • 903
  • 8
  • 23
Kalpesh Rupani
  • 991
  • 4
  • 12