简体   繁体   English

批量推送Firebase注销

[英]Firebase Logout On Batch Push

I am creating an app on Android that uses Firebase as database and Batch for pushing notifications. 我正在Android上创建一个应用,该应用使用Firebase作为数据库,并使用Batch来推送通知。 Usually, when my app starts, it goes to the main page, a login activity. 通常,当我的应用程序启动时,它会转到主页,即登录活动。 The activity verifies if a user is still logged in using: 该活动使用以下方法验证用户是否仍在登录:

Firebase dbRef = new Firebase(Constants.URL_DB);
AuthData auth = dbRef.getAuth();

if (auth != null) // Proceed with a logged in user
else // Show authentication layout

My problem is that when I get a notification from Batch, I click on the notification to go to the app but then I am not logged in as I should be... auth == null . 我的问题是,当我从Batch收到通知时,我单击该通知以转到该应用程序,但是我没有登录,因为我应该是... auth == null I don't want my users to need to log in every time they get a push from Batch. 我不希望我的用户每次从Batch获得推送时都需要登录。 Can I detect that the app started from a notification? 我可以检测到该应用程序是从通知启动的吗? How is that I lose authentication from Firebase? 我如何从Firebase丢失身份验证?

Here is the onCreate and onResume of the MainActivity: 这是MainActivity的onCreate和onResume:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initiating Batch
    Batch.onStart(this);

    // Initiating layout
    setContentView(R.layout.login);

    // Setting database
    Firebase.setAndroidContext(this);

    // Unrelated stuff done here (Setting Views, etc)
}

@Override
protected void onResume() {
    super.onResume();

    // Getting login information from previous authentication.
    Firebase dbRef = new Firebase(Constants.URL_DB);
    AuthData auth = dbRef.getAuth();

    // I added the addAuthStateListener here

    if (auth != null) {
        goToHomePage();
    }
}

All right I found the problem. 好吧,我发现了问题。 When I click on the notification, my MainActivity is called obviously. 当我单击通知时,显然会调用我的MainActivity。 The thing is that when the user is logged in successfully, I start another Activity using: 问题是,当用户成功登录后,我将使用以下命令启动另一个活动:

startActivityForResult(intent, Constants.SUCCESS);

Now, onActivityResult is normally called to log out the user when the back button has been pressed on the home page. 现在,通常会在按下主页上的后退按钮后调用onActivityResult以注销用户。 Otherwise, onResume is called and since the user is logged in, I would go straight back to the home page. 否则,将调用onResume ,并且由于用户已登录,因此我将直接回到主页。 BUT: when I click on a notification, somehow onActivityResult is called (probably because the activity stack gets trashed) and the user is logged out before resuming the activity. 但是:当我单击通知时,会以某种方式调用onActivityResult (可能是因为活动堆栈已被破坏),并且用户在恢复活动之前已注销。

So the solution is to log out the user in the onBackPressed of the home page activity. 因此,解决方案是在主页活动的onBackPressed中注销用户。 Then I don't need to startActivityForResult anymore. 然后,我不再需要startActivityForResult了。

// In the home page activity
@Override
public void onBackPressed() {
    super.onBackPressed();
    Firebase dbRef = new Firebase(Constants.URL_DB);
    dbRef.unauth();
    finish();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM