반응형
📌 선행 작업
발급받은 serviceAccountKey.json 파일로 초기 세팅해주기.
import admin from 'firebase-admin';
import serviceAccount from 'path/to/serviceAccountKey.json';
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
1. 단일 건 전송
admin.messaging().send() 사용
token 키 값에 푸시 메시지를 보낼 단일 token 값 설정
import admin from 'firebase-admin';
const token = 'token123456789';
const message = {
token: token,
data: { key1: 'value1', key2: 'value2' },
notification: {
title: 'notification title',
body: 'notification body content',
},
android: {
priority: 'high',
...
},
apns: {
payload: {
aps: {
sound: 'default',
...
},
},
}
};
await admin.messaging().send(message);
2. 여러 건 벌크로 전송
admin.messaging().sendEachForMulticast() 사용
tokens 키 값에 푸시 메시지를 보낼 token 리스트 전달
import admin from 'firebase-admin';
const tokenList = [
'token111111111',
'token222222222',
'token333333333',
'token444444444',
'token555555555',
];
const message = {
tokens: tokenList,
data: { key1: 'value1', key2: 'value2' },
notification: {
title: 'notification title',
body: 'notification body content',
},
android: {
priority: 'high',
...
},
apns: {
payload: {
aps: {
sound: 'default',
...
},
},
}
};
await admin.messaging().sendEachForMulticast(message);
'Node.js' 카테고리의 다른 글
mongodb atlas whitelist에 0.0.0.0을 넣어도 연결이 안될 때 (0) | 2025.06.07 |
---|---|
[Node.js] Node에서 replaceAll 사용하기 (0) | 2024.08.22 |
[Node.js] PM2로 프로젝트 구동 시 개발/상용 환경 나누기 (0) | 2024.07.18 |
[Node.js] FCM으로 푸시알림 보내는 API 작성하기 (0) | 2024.07.10 |
[Cheerio] 현재 태그의 텍스트만 가져오기 (0) | 2024.06.19 |