본문 바로가기
AWS :

[FN] 모임 신청 개발 일지[2] / notifee, API - EC2 배포

by 밍코딩코 2025. 2. 23.

클라이언트 -> 서버 요청 -> 사용자 알림 구현은 이전 게시글 참고하기

 

[FN] 모임 신청 개발 일지[1] / Spring Boot - FCM 푸시 알림 전송

클라이언트에서 Spring Boot 서버로 FCM 토큰을 보내는 테스트 과정은 아래 게시글 참고하기 [FN] Spring Boot API / FCM Token 전송모임 신청을 하고 신청에 대한 알림을 FCM으로 모임장에게 띄워주기 위해

codingco.tistory.com

 

이번에는 notifee를 사용한 기기에 실제 푸시 알림을 구현해보자

 

1. notifee

이전 테스트 방식은 콘솔 메시지에 신청서 내용이 모두 찍히도록 했었다.

const unsubscribeOnMessage = messaging().onMessage(async remoteMessage => {
      console.log('포그라운드 알림 메시지:', remoteMessage);
}

    
    messaging().setBackgroundMessageHandler(async remoteMessage => {
      console.log('백그라운드 알림 메시지:', remoteMessage);

 

수정된 코드로는 notifee를 사용해서 기기에 푸시 알림으로 나타나도록 수정함

안드로이드에서 알림을 띄우기 위해 notifee 채널을 생성해야한다.

useEffect(() => {

    async function createChannel() {
      await notifee.createChannel({
        id: 'default',
        name: '기본 채널',
        sound: 'default',
        vibration: true,
      });
    }

    createChannel();

 

채널을 생성했으면 displayNotification 으로 실제 알림을 띄운다 / 포그라운드, 백그라운드 코드는 같다.

const unsubscribeOnMessage = messaging().onMessage(async remoteMessage => {

      
      await notifee.displayNotification({
        title: remoteMessage.notification?.title,
        body: remoteMessage.notification?.body,
        android: {
          channelId: 'default',
        },
      });
    });

 

2. EC2에 배포

인스턴스 생성 방법은 아래 게시글 참고

 

[FN] 게시판 개발 일지[5] / RESTful API, AWS EC2

게시글 저장은 Firestore database 와 AWS S3에 이미지 업로드 되도록 했어서RESTful API를 활용해 보기 위해서 게시글 조회와 검색 기능에 적용하였다. 1. 시작Node.js 프로젝트 초기화npm init -y 필요한 프

codingco.tistory.com

Intellij 터미널에서 .jar 파일을 생성한다. .jar 파일을 EC2에 배포해야함

./gradlew bootJar

.jar 파일이 생성됨

Powershell 을 키고 .pem 파일이 있는 경로로 이동

cd C:\Intellij

 

SSH 접속

ssh -i "FestaNow-SpringBoot-meeting.pem" ec2-user@EC2-IP주소

 

Spring Boot는 java가 필요하므로 EC2에 JDK 설치

sudo yum update -y
sudo yum install java-17-amazon-corretto -y

 

생성했던 .jar 파일 업로드하기 (exit로 SSH 접속 해제하고 업로드해야 함)

scp -i "C:/Intellij/FestaNow-SpringBoot-meeting.pem" C:/Intellij/festanow-meeting/build/libs/festanow-meeting-0.0.1-SNAPSHOT.jar ec2-user@EC2-IP주소:/home/ec2-user/

 

다시 SSH 접속 후 파일 저장 확인하기

ls -l /home/ec2-user/

 

파일 업로드 경로로 이동

cd /home/ec2-user/

 

실행 권한 설정

chmod +x /home/ec2-user/festanow-meeting-0.0.1-SNAPSHOT.jar

 

실행

java -jar /home/ec2-user/festanow-meeting-0.0.1-SNAPSHOT.jar

 

 

3. systemd로 자동 실행

서비스 파일 생성

sudo nano /etc/systemd/system/festanow-meeting.service

 

아래 코드 추가

  • ExecStart: Spring Boot 애플리케이션을 실행할 명령어
  • User=ec2-user: 애플리케이션을 실행할 사용자
  • Restart=always: 애플리케이션이 종료되면 자동으로 다시 시작되도록 설정
  • TimeoutStopSec=10: 애플리케이션 종료 대기 시간을 설정
  • Environment: EC2에 java 경로 설정하기
[Unit]
Description=Spring Boot Application
After=network.target

[Service]
User=ec2-user
ExecStart=/usr/bin/java -jar /home/ec2-user/festanow-meeting-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143
TimeoutStopSec=10
Restart=always
WorkingDirectory=/home/ec2-user
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk"
Environment="PATH=$JAVA_HOME/bin:$PATH"

[Install]
WantedBy=multi-user.target

 

서비스 등록 / 실행

sudo systemctl daemon-reload
sudo systemctl enable festanow-meeting.service
sudo systemctl start festanow-meeting.service

 

상태 확인

sudo systemctl status festanow-meeting.service

 

잘 실행된다

서비스를 중지하려면

sudo systemctl stop festanow-meeting.service

 

재시작하려면

sudo systemctl restart festanow-meeting.service

 

 

 

 

 

"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

'AWS :' 카테고리의 다른 글

[FN] 게시판 개발 일지[5] / RESTful API, AWS EC2  (0) 2025.02.14
[FN] AWS S3, AWS IAM  (8) 2025.01.19