gpt4 book ai didi

android - Firebase Auth SignOut 不适用于卸载

转载 作者:行者123 更新时间:2023-12-05 00:09:59 25 4
gpt4 key购买 nike

我正在使用 Sign-in with Google 方法在我的应用中登录用户。

问题 - 更新 :

  • 如果我卸载应用程序(在退出后或未退出)并重新安装应用程序,FirebaseAuth.getInstance().getCurrentUser() 将非空。因此,用户可以访问该帐户。
  • 我什至尝试通过清除应用程序数据,问题仍然存在。

  • 该应用在添加“FirebaseInstanceIdService”和“FirebaseMessagingService”服务之前运行良好。意思是卸载后自动退出。

    list .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();
    }

    最佳答案

    你不是唯一一个为此苦苦挣扎的人。我有类似的问题。重新安装(或更新)应用程序后,在 BaseActivity 中我检查了用户是否已登录。他是.. FirebaseAuth.getInstance().getCurrentUser()返回非空值。所以当我使用 uid为了从 firebase 数据库中获取数据,我弄得一团糟——一些随机项目。

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

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

    事实 #1 (docs)

    There are some cases where getCurrentUser will return a non-null FirebaseUser but the underlying token is not valid.



    事实上,当 getCurrentUser() != null你不能确定用户是否真的登录了。我的结论是,我不能依赖 getCurrentUser我应该重新考虑如何检查用户是否登录( as an option)。

    事实 #2

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

    解决方案
    <application
    android:allowBackup="false"
    android:fullBackupContent="false"
    ...>

    我设置 android:allowBackupandroid:fullBackupContent为假。所以当我重新安装应用程序(或更新它)时,firebase 当前用户是 null .

    关于android - Firebase Auth SignOut 不适用于卸载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50259911/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com