<aside> 💡 fcm 과 sns 연동하여 iOS, AOS 모두 전송 푸시 알림은 SNS로 위임하여 전송되도록 하기 위해 분리 iOS APN을 firebase에 등록하여 GCM 포맷으로 전송해도 전송된다.

</aside>

아키텍처

Untitled

기능 흐름

  1. 서버에서 푸시서버로 알림에 필요한 메시지, 유저의 id 등 메타 데이터를 보내준다.
  2. 푸시서버에서는 유저 id를 통해 fcm 토큰, 디바이스 타입(iOS, AOS) 를 조회를 한다.
  3. 2에서 조회한 정보로 각 디바이스에 맞는 메시지 포맷으로 AWS SNS 에 publish 를 해준다.
  4. AWS SNS 는 firebase api 와 연동되어 있고 publish 된 정보를 토대로 firebase 에 전송한다.
  5. firebase 는 각 디바이스에 알림을 전송한다.

예제 코드

import { INestApplication } from '@nestjs/common';
import { initTestingApp } from 'test-e2e/common/InitTestingApp';
import AWS from 'aws-sdk';

describe('푸쉬 알림 서버 구축', () => {
  let app: INestApplication;

  const SNS_ACESS_KEY = 'SNS_ACESS_KEY';
  const SNS_SECRET_KEY = 'SNS_SECRET_KEY';
  //AOS FCM 토큰
  const AOS_TOKEN = 'AOS_TOKEN';

	//IOS FCM 토큰
  const iOS_TOKEN = 'iOS_TOKEN';

  const REGION = 'ap-northeast-2';
  const PlatformApplicationArn = 'arn';
  AWS.config.update({
    accessKeyId: SNS_ACESS_KEY,
    secretAccessKey: SNS_SECRET_KEY,
    region: REGION,
  });
  const awsSNS = new AWS.SNS();
  beforeAll(async () => {
    app = await initTestingApp();
  });

  it('sns push 서비스', async () => {
    const params = {
      PlatformApplicationArn,
      Token: AOS_TOKEN,
    };
    

    const payload2 = { GCM: {} };

    payload2.GCM = {
      notification: {
        body: '메시지',
        title: '타이틀',
      },
    };

    payload2.GCM = JSON.stringify(payload2.GCM);

    const createdEndPoint = await awsSNS.createPlatformEndpoint(params).promise();
    await awsSNS
      .publish({
        Message: JSON.stringify(payload2),
        MessageStructure: 'json',
        TargetArn: createdEndPoint.EndpointArn,
      })
      .promise();
  });

  it('iOS FCM 테스트', async () => {
    const params = {
      PlatformApplicationArn,
      Token: iOS_TOKEN,
    };

    const payload2 = { GCM: {} };

    payload2.GCM = {
      notification: {
        body: 'i바디',
        title: '타이틀',
      },
    };

    payload2.GCM = JSON.stringify(payload2.GCM);

    const createdEndPoint = await awsSNS.createPlatformEndpoint(params).promise();
    await awsSNS
      .publish({
        Message: JSON.stringify(payload2),
        MessageStructure: 'json',
        TargetArn: createdEndPoint.EndpointArn,
      })
      .promise();
  });
});