Firebase Auth SignOut无法解除卸载

时间:2018-05-09 18:29:19

标签: android firebase firebase-authentication firebase-cloud-messaging

我使用Google方式登录在我的应用中登录用户。

问题 - 更新

  1. 如果我卸载应用程序(在注销或未注销后)并重新安装应用程序,则FirebaseAuth.getInstance()。getCurrentUser()将为非null。因此,用户可以访问该帐户。

  2. 我甚至尝试清除应用数据,问题仍然存在。

  3. 在添加' FirebaseInstanceIdService'之前,应用程序运行良好。和' FirebaseMessagingService'服务。意思是,它在卸载后自动退出。

    的Manifest.xml

        <meta-data   
    android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id"/>
        <meta-data android:name="firebase_messaging_auto_init_enabled"
            android:value="false" />
        <meta-data android:name="firebase_analytics_collection_enabled"
            android:value="false" />
    
    ....
    
    <service
            android:name=".services.MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
    
        <service
            android:name=".services.MyFirebaseInstanceIDService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
    
    ....
    

    MyFirebaseMessagingService.class

    public class MyFirebaseMessagingService extends FirebaseMessagingService {
    
    private static final String TAG = "MessagingService";
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
    
        Log.e(TAG, String.valueOf(remoteMessage));
    }
    }
    

    MyFirebaseInstanceIDService.class

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    
    private static final String TAG = "InstanceIdService";
    
    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
    
        if (FirebaseAuth.getInstance().getCurrentUser() == null){
            return;
        }
    
        String tokenId = FirebaseInstanceId.getInstance().getToken();
    
        saveTokenIdToDatabase(tokenId);
    }
    }
    

    signOutAccount方法

    private void signOutAccount(){
    
    FirebaseAuth.getInstance.signOut();
    
                Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finishAffinity();
    }
    

1 个答案:

答案 0 :(得分:3)

你并不是唯一一个与之斗争的人。我有类似的问题。重新安装(或更新)应用程序后,在BaseActivity中我检查了用户是否已登录。他是.. FirebaseAuth.getInstance().getCurrentUser()返回非空值。因此,当我使用uid从firebase数据库获取数据时,我得到了一些乱七八糟的东西。

另请注意,我只能在某些设备上重现。例如,我在 Nexus 5x 上获得了非空值,但在 Oneplue 3t (8.0.0)上没有。

经过一番调查后,我发现了一些有趣的事实。

事实#1 docs

  

在某些情况下,getCurrentUser将返回非null值   FirebaseUser但基础令牌无效。

事实上,getCurrentUser() != null时,您无法确定用户是否真的已登录。我的结论是,我不能依赖getCurrentUser,我应该重新考虑这种方式我如何检查用户是否登录(as an option)。

事实#2

Android应用有时会在重新安装应用后记住其数据,因为 自动备份。 See the post for more details

<强>解决方案

<application
  android:allowBackup="false"
  android:fullBackupContent="false"
  ...>

我将android:allowBackupandroid:fullBackupContent设为false。因此,当我重新安装应用程序(或更新它)时,firebase当前用户为null

相关问题