Search in sources :

Example 1 with FirebaseMessagingException

use of com.google.firebase.messaging.FirebaseMessagingException in project zhcet-web by zhcet-amu.

the class FirebaseErrorParsingUtils method getTokenStatus.

/**
 * Checks if a firebase token is invalid by checking if it either expired or not forbidden
 * @param throwable Throwable of firebase execution error
 * @return TokenStatus denoting status of the token
 */
public static TokenStatus getTokenStatus(Throwable throwable) {
    Throwable current = throwable;
    while (!(current instanceof FirebaseMessagingException) && current != null) {
        current = current.getCause();
    }
    if (current == null)
        throw new InvalidThrowableException(throwable);
    // We have a FirebaseMessagingException
    FirebaseMessagingException firebaseMessagingException = (FirebaseMessagingException) current;
    while (!(current instanceof HttpResponseException) && current != null) {
        current = current.getCause();
    }
    if (current == null)
        throw new InvalidThrowableException(throwable);
    // We have a HttpResponseException
    HttpResponseException httpResponseException = (HttpResponseException) current;
    int statusCode = httpResponseException.getStatusCode();
    Reason reason = new Reason(statusCode, current.getMessage(), httpResponseException.getContent());
    boolean isTokenExpired = statusCode == 404 || statusCode == 400;
    boolean isTokenForbidden = statusCode == 403;
    if (isTokenExpired || isTokenForbidden) {
        return new TokenStatus(false, reason);
    } else {
        return new TokenStatus(true, reason);
    }
}
Also used : HttpResponseException(com.google.api.client.http.HttpResponseException) FirebaseMessagingException(com.google.firebase.messaging.FirebaseMessagingException)

Aggregations

HttpResponseException (com.google.api.client.http.HttpResponseException)1 FirebaseMessagingException (com.google.firebase.messaging.FirebaseMessagingException)1