FCM 토큰 사용하여 사용자에게 push 알림 보내기 (3) - FCM 백엔드가 어플에 push 어떻게 보내는데 ?

like0·2022년 4월 8일
0

지난 글에서
앱 서버 또는 신뢰할 수 있는 서버환경은 FCM과 Firebase Admin SDK 또는
원시 프로토콜 (FCM HTTP v1 API, 기존 HTTP 프로토콜, XMPP 서버 프로토콜)로 상호작용한다는 이야기를 했다.

그리고 본인은 그 중에서 Firebase Admin SDK를 사용하였고, 그래서 SDK 추가와 초기화까지 언급을 하였다!

그럼 이번 글에서는 FCM이 어떻게 어플에 push를 하는지 적어보려고 한다!

앱 서버 전송 요청 작성

sendToDevice 함수를 통해, db에 저장된 해당 user의 token에 메시지를 보낼 수 있다.

// This registration token comes from the client FCM SDKs.
const registrationToken = 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...';

// See the "Defining the message payload" section below for details
// on how to define a message payload.
const payload = {
  data: {
    score: '850',
    time: '2:45'
  }
};

// Send a message to the device corresponding to the provided
// registration token.
getMessaging().sendToDevice(registrationToken, payload)
  .then((response) => {
    // See the MessagingDevicesResponse reference documentation for
    // the contents of response.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });
profile
배우고 성장하는 개발자가 되기!

0개의 댓글