错误:找不到符号类 FirebaseInstanceIdService

Posted

技术标签:

【中文标题】错误:找不到符号类 FirebaseInstanceIdService【英文标题】:error: cannot find symbol class FirebaseInstanceIdService 【发布时间】:2020-05-03 03:04:22 【问题描述】:
    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();
    
    

这是登录活动代码 我收到一个错误 找不到符号类 FirebaseInstanceIdService 我附上一张图片供参考

这是我最后一年的项目,它是一个基于聊天的应用程序 由于这个过程,我无法生成 APK 但 Gradle 同步成功。

【问题讨论】:

FirebaseInstanceIdService 已弃用。你应该使用FirebaseMessagingService 这能回答你的问题吗? FirebaseInstanceIdService is deprecated 【参考方案1】:

开发人员被 FirebaseInstanceIdService 弃用的问题所困扰,那么现在该怎么办?

FirebaseInstanceIdService

这个类已被弃用。 赞成在FirebaseMessagingService 中覆盖onNewToken。实施后,可以安全地删除此服务。 这意味着无需使用 FirebaseInstanceIdService 服务来获取 FCM 令牌。您可以安全地移除 FirebaseInstanceIdService 服务

现在我们需要@Override onNewToken()在“FirebaseMessagingService”中获取Token。

示例代码:

    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);
        
     

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(); 也被弃用,如果你需要在你的 Activity 中获取令牌然后使用如下:

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

         
     );

我希望你能在这里得到你的解决方案。

【讨论】:

使用 onComplete(@NonNull Task task) 怎么样?

以上是关于错误:找不到符号类 FirebaseInstanceIdService的主要内容,如果未能解决你的问题,请参考以下文章

java 中 “找不到符号”的错误

错误:在 ParseQuery.or() 中找不到符号类“或”;

找不到符号类RTCClientVideoTracksCallback

在 android 中使用 MVVM 设计模式时出错找不到符号类 ViewModel

使用java的jar包默认类提示找不到符号

java中提示找不到符号