Android – 当从通知打开活动时,getCurrentUser() 返回null。

huangapple 未分类评论43阅读模式
英文:

Android - getCurrentUser() returns null when opening an activity from notification

问题

我有一个应用程序,如果用户已登录,他将收到通知。当他点击通知时,他将被重定向到HomeActivity。我有一个在每次打开应用程序时运行的类,用于检查用户是否已登录。如果他已登录,他将被引导到HomeActivity,否则将被引导到RestaurantAuthActivity。
问题是,当我登录用户并点击收到的通知时,它会将我重定向到特定的活动,但当我关闭应用程序并尝试重新启动时,它会将我重定向到RestaurantAuthActivity。经过检查,我发现当我从通知中被引导到HomeActivity时,getCurrentUser() 返回null,而它不应为null。
在HomeActivity中,我有一个单独的“登出”按钮,用于登出用户。
在每次启动应用程序时,Home.class 都会检查用户是否已登录。

public class Home extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();

        SharedPreferences sharedPreferences = getSharedPreferences("RestaurantInfo", MODE_PRIVATE);
        String restaurantID = sharedPreferences.getString("RestaurantID", "");

        if (firebaseUser != null) {

            System.out.println("FirebaseUSER: " + firebaseUser);
            Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);

        } else {
            System.out.println("FirebaseUSER inside false: " + firebaseUser);
            Intent intent = new Intent(getApplicationContext(), RestaurantAuthActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

    }

}

根据XML,RestaurantAuthActivity 是我的启动活动。之所以仍然将我引导到HomeActivity,是因为在RestaurantAuthActivity 中有以下代码片段。

if (getIntent().hasExtra("page")) {

        System.out.println("Inside getIntent()");
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

有人能帮助我解决这个问题吗!

英文:

I've an app where if a user is signed in, he will receive notifications. When he clicks on the notifications he will be redirected to a HomeActivity. I have a class that runs whenever the app is opened, to check if a user is signed in or not. If he is signed in he will be directed to HomeActivity or else RestaurantAuthActivity.
The issue is when I sign in the user and click on the notification received, it is redirecting me that specific activity, but when I close the app and try to start it again, it redirects me to RestaurantAuthActivity. On checking, I see that when I'm directed to HomeActivity form the notification, the getCurrentUser() returns null, which should not be null.
I've a separate "Sign Out" button in HomeActivity which signs out the user.
Home.class checks if a user is signed in or not whenever the app is started

public class Home extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();

        SharedPreferences sharedPreferences = getSharedPreferences("RestaurantInfo", MODE_PRIVATE);
        String restaurantID = sharedPreferences.getString("RestaurantID", "");

        if (firebaseUser != null) {

            System.out.println("FirebaseUSER: " + firebaseUser);
            Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);

        } else {
            System.out.println("FirebaseUSER inside false: " + firebaseUser);
            Intent intent = new Intent(getApplicationContext(), RestaurantAuthActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

    }

}

As per XML, the RestaurantAuthActivity is my launcher activity. The reason why it is still directing me to HomeActivity is because of the below code snippet in RestaurantAuthActivity.

if (getIntent().hasExtra("page")) {

        System.out.println("Inside getIntent()");
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

Could someone help me on this!

huangapple
  • 本文由 发表于 2020年4月4日 00:43:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/61016572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定