GO SDK를 이용한 FCM (FirseBase Message Push)

PIZZU·2024년 2월 22일
0

SDK를 이용하여 Token으로 Push 보내는 법

아래 이미지와 같이 firebase > 프로젝트 설정 > 서비스 계정 > 새 비공개 키 생성 에서 json파일을 받아 저장한다.
(나는 fcm_auth.json 이라는 이름으로 저장함)

push.go

package handler

import (
	"context"
	"fmt"
	"log"

	firebase "firebase.google.com/go/v4"
	"firebase.google.com/go/v4/messaging"
	"github.com/gin-gonic/gin"
	"google.golang.org/api/option"
)

func FCMPushAPI(c *gin.Context) {
	fmt.Println("fcm push api")
	opt := option.WithCredentialsFile("./fcm_auth.json")

	app, err := firebase.NewApp(context.Background(), nil, opt)

	if err != nil {
		log.Fatalf("Error initializing Firebase app: %v\n", err)
	}

	// Firebase Cloud Messaging 클라이언트 초기화
	client, err := app.Messaging(context.Background())
	if err != nil {
		log.Fatalf("Error initializing Firebase Cloud Messaging client: %v\n", err)
	}

	// 메시지 생성
	message := &messaging.Message{
		Data:         map[string]string{},
		Notification: &messaging.Notification{Title: "Welcome Title", Body: "Welcome Content!"},
		Token:        "DB에 저장된 유저의 PUSH Token",
	}

	// 메시지 전송
	response, err := client.Send(context.Background(), message)
	if err != nil {
		log.Fatalf("Error sending message: %v\n", err)
		fmt.Println(err)
	}

	// 응답 출력
	fmt.Println("Successfully sent message:", response)
}

아래 링크 : php Http v1을 이용한 FCM push 보내는 법
php http v1 FCM Push

profile
pizzu's blog

0개의 댓글