Search in sources :

Example 1 with GetTokenResult

use of com.google.firebase.auth.GetTokenResult in project iosched by google.

the class RegistrationStatusService method getFirebaseToken.

/**
 * Get Firebase token for current user
 */
private String getFirebaseToken() throws Exception {
    FirebaseUser fbUser = mFirebaseAuth.getCurrentUser();
    if (fbUser == null) {
        throw new IOException("Firebase user not authenticated");
    }
    // Get Firebase token and wait for operation to complete
    final CountDownLatch latch = new CountDownLatch(1);
    Task<GetTokenResult> task = fbUser.getToken(false);
    task.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {

        @Override
        public void onComplete(@NonNull Task<GetTokenResult> task) {
            latch.countDown();
        }
    });
    latch.await();
    if (task.isSuccessful()) {
        String fbToken = task.getResult().getToken();
        if (fbToken == null) {
            throw new IOException("Received null Firebase token");
        }
        return fbToken;
    } else {
        throw task.getException();
    }
}
Also used : GetTokenResult(com.google.firebase.auth.GetTokenResult) FirebaseUser(com.google.firebase.auth.FirebaseUser) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

FirebaseUser (com.google.firebase.auth.FirebaseUser)1 GetTokenResult (com.google.firebase.auth.GetTokenResult)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1