package com.remax.visualnovel.app.initializer.utils

import com.google.android.gms.tasks.Task
import com.google.firebase.messaging.FirebaseMessaging
import timber.log.Timber

/**
 * Created by HJW on 2022/10/18
 */
object FirebaseHelper {

    fun getToken(tokenCallback: ((String?) -> Unit)? = null) {
        FirebaseMessaging.getInstance().token.addOnCompleteListener { task: Task<String?> ->
            try {
                Timber.d("firebase CompleteListener isSuccessful : %s", task.isSuccessful)
                if (task.isSuccessful && task.result != null) {
                    val token = task.result
                    Timber.d("firebase token : %s", token)
                    tokenCallback?.invoke(token)
                } else {
                    tokenCallback?.invoke(null)
                }
            } catch (e: Exception) {
                Timber.d("firebase token exception: %s", e.localizedMessage)
                tokenCallback?.invoke(null)
            }
        }
    }
}