2016-03-24 48 views
0

我正在使用Firebase作爲數據庫和批量推送通知的Android上創建應用程序。通常,當我的應用程序啓動時,它會轉到主頁面,一個登錄活動。活動驗證用戶是否使用仍處於登錄:Firebase註銷批量推送

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

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

我的問題是,當我得到批量的通知,我點擊通知去應用程序,但隨後我沒有登錄爲我應該是... auth == null。我不希望我的用戶在每次從批量推送時都需要登錄。我可以檢測到應用程序是從通知開始的嗎?我如何失去Firebase的身份驗證?

這裏是在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(); 
    } 
} 
+0

嗯......聽起來很不尋常。你可以嘗試用偵聽器來檢測身份驗證:https://www.firebase.com/docs/android/guide/user-auth.html#section-monitoring-authentication它應該做同樣的事情,但我想知道,在狀態已知之前,以某種方式調用'getAuth()'。 –

+0

這裏沒有足夠的代碼來猜測所有這些發生的時間。請參閱[請教](http://stackoverflow.com/help/how-to-ask)和[創建mcve](http://stackoverflow.com/help/mcve) – Kato

+0

authData在'public void onAuthStateChanged(AuthData authData){...}'因爲我沒有登錄 – Sunshinator

回答

0

好吧,我發現這個問題。當我點擊通知時,我的MainActivity顯然被調用。問題是,當用戶成功登錄,我用再掀活動:現在

startActivityForResult(intent, Constants.SUCCESS);

onActivityResult通常被稱爲當返回按鈕被按下主頁上登出的用戶。否則,onResume被調用,並且由於用戶登錄,我會直接回到主頁。但是:當我點擊通知時,不知何故onActivityResult被調用(可能是因爲活動堆棧被刪除),並且用戶在恢復活動之前被註銷。

因此,解決方案是註銷主頁活動的onBackPressed中的用戶。然後我不再需要startActivityForResult了。

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