package com.fdkankan.push.utils;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;

/**
 * Created by Hb_zzZ on 2020/7/27.
 */
@Slf4j
public class PushMsgUtil {

    /**
     * 海外谷歌推送
     */
    public static void googlePushMsg(String refreshTokenJsonFileUrl, String token, String title, String text, String url){
        try{
            FileInputStream refreshToken = new FileInputStream(refreshTokenJsonFileUrl);

            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(refreshToken))
                    .setDatabaseUrl("https://dkankan-pro.firebaseio.com")
                    .build();

            FirebaseApp.initializeApp(options);

            com.google.firebase.messaging.Message message = com.google.firebase.messaging.Message.builder()
                    .putData("titile", title)
                    .putData("text", text)
                    .putData("url", url)
                    .setNotification(new com.google.firebase.messaging.Notification(title, text))
                    .setToken(token)
                    .build();
            String response = FirebaseMessaging.getInstance().send(message);
            log.info("Successfully sent message: " + response);
        }catch (Exception e ){
            e.printStackTrace();
        }

    }
}
