package com.zxdemo.utils
import android.content.Context
import android.util.Log
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.FirebaseApp
import com.google.firebase.messaging.FirebaseMessaging
import com.zxdemo.http.ZxHttp


class FcmUtils {

    fun initFirebase(context: Context?) {
        FirebaseApp.initializeApp(context!!)
    }

    fun subscribeToTopic(topic: String) {
        Log.d("TAG", "subscribeToTopic: $topic")
        FirebaseMessaging.getInstance().subscribeToTopic(topic).addOnCompleteListener {
            Log.d("TAG", "subscribeToTopic:isSuccessful ${it.isSuccessful}")
            if (it.isSuccessful) {
                ZxHttp.getHttpReportInterface("FCM_Topic$topic", "", null)
                Log.d("FCMUtil", "suc")
            } else {
                Log.d("FCMUtil", "fail")
            }
        }
    }

    fun unsubscribeFromTopic(topic: String) {
        FirebaseMessaging.getInstance().unsubscribeFromTopic(topic)
            .addOnCompleteListener(OnCompleteListener<Void?> { task ->
                if (task.isSuccessful) {
                } else {
                }
            })
    }
}