728x90
FCM ์ค์
- ๊ณต์ ์์ ์์๋ Node JS ๊ธฐ์ค์ผ๋ก ์ค๋ช
๋์ด ์์
https://firebase.google.com/docs/cloud-messaging/js/client?hl=ko&_gl=1*16582yh*_up*MQ..*_ga*MTk1Nzk3ODc5Mi4xNzEzMjI3MTcy*_ga_CW55HF8NVT*MTcxMzIyNzE3Mi4xLjAuMTcxMzIyNzE3Mi4wLjAuMA..#web-modular-api - Vanilla JS ํ๊ฒฝ์์ ํ๋ ๋ฒ ์ ๋ฆฌ
1. Firebase CDN ์ถ๊ฐ
์ฌ๋ฌ ๋ฐฉ๋ฒ์ ์๋ํด๋ดค๋๋ฐ ๊ฐ์ฅ ์ ์์ ์ผ๋ก ๋์ํ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.
<!-- index.html -->
<script type="module">
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.11.0/firebase-app.js'
import { getMessaging, getToken, onMessage } from 'https://www.gstatic.com/firebasejs/10.11.0/firebase-messaging.js'
...
์ฒ์์๋ CDN ํ์ฉํด์
<script src="https://cdnjs.cloudflare.com/ajax/libs/firebase/10.11.0/firebase-app.js" integrity="sha512-JcwDQxh56yOxNHIafGu2ykBz2TUF9vsGSySz72SwfN4XSm/4jls/vPWV5wPWKTekXPtPHejHN7jUBCdJB0Yehw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
์ด๋ ๊ฒ๋ ํด๋ดค๋๋ฐ, ์ ๋๋ก ๋์ํ์ง ์์์ module
๋ฐฉ์์ผ๋ก ํ์ํ ๋ถ๋ถ์ ๊ตฌํํ๊ธฐ๋ก ํ๋ค.
2. Firebase Config ์ถ๊ฐ ๋ฐ ์ด๊ธฐํ
- ํ๋ก์ ํธ ์ค์ ์์ config๋ฅผ ์ป์ ์ ์์.
// index.html
// appKey = VAPID Key
const appKey = "YOUR_VAPID_KEY";
const config = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID",
measurementId: "YOUR_MEASUREMENT_ID",
};
initializeApp(config);
const messaging = getMessaging();
๊ทธ๋ฆฌ๊ณ ์๋ฆผ ๊ถํ์ ๋ฐ๋๋ค. ๊ณผ๊ฑฐ firebase ๋ฒ์ ์์๋ messaging์ ํตํด์ ๊ถํ์ ๋ฐ์๋ ๊ฒ ๊ฐ์๋ฐ, ๊ทผ๋์๋ ๋ฐ๋์๋ค.
// index.html
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
console.log('Notification permission granted.');
if (isTokenSentToServer()) {
console.log('Token already sent to server');
}
} else {
console.log('Unable to get permission to notify.');
}
}).catch(function (err) {
console.log('Unable to get permission to notify.', err);
});
3. ์น ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด ๊ตฌ์ฑ
- ์๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ์๋ฒ ID(VAPID ํค) ๋ผ๋ ์น ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ํตํด ์น ํธ์ ์๋น์ค ๋ณด๋ด๊ธฐ ์์ฒญ ์น์ธ
- ํค ์์ ์์ฑํ๊ฑฐ๋ ๊ธฐ์กด ๊ฒ์ ๊ฐ์ ธ์ฌ ์ ์์
- FCM์ ์ฐ๊ธฐ ์ํด์
Server Key(VAPID key)
๋ผ๋ ๊ฒ ํ์ํ๋ฐ, ์ด๊ฑธ ์ํด์ Cloud Messaging API(๊ธฐ์กด) ์ด๋ผ๊ณ ํ๋์ด์๋ API๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค. ์ ์ธ ๊ฐ ๋๋ฅด๊ณ API ๊ด๋ฆฌ์๋ก ํ๋ก์ ํธ์ ์ถ๊ฐํ์. - ๊ทธ๋ฌ๊ณ ๋๋ฉด Server Key๊ฐ ๋์จ๋ค.
4. getToken์ ํตํด์ ์ฌ์ฉ์ ์ธ์ฆ
getToken
์ ์ด์ฉํด์ ๊ธฐ๊ธฐ ์๋ณ์ฉ ํ ํฐ์ ๋ง๋ค๊ณ ๋ฐ์์ฌ ์ ์๋ค.vapidKey
์ server key๋ฅผ ๋ฃ์ผ๋ฉด ๋๋ค.
getToken(messaging, { vapidKey: 'YOUR_VAPID_KEY' })
.then(function (currentToken) {
if (currentToken) {
console.log(currentToken);
setTokenSentToServer(true, currentToken);
} else {
setTokenSentToServer(false);
}
}).catch(function (err) {
console.log('An error occurred while retrieving token. ', err);
showToken('Error retrieving Instance ID token. ', err);
setTokenSentToServer(false);
});
5. firebase-messaging-sw.js ๋ฑ๋ก
- FCM ์ฌ์ฉ์ ์ํด์๋
firebase-messaging-sw.js
๋ผ๋ ํ์ผ์ด ๋ฐ๋์ ์กด์ฌํด์ผํ๋ค. ์ผ๋จ ๋ญ ์์ํ๋ ค๊ณ ํ๋ฉดhostname/firebase-messaging-sw.js
ํ์ผ์ ์ฐพ์ผ๋ฏ๋ก ๋ฃจํธ ์๋์์ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ํด์ค์ผํ๋ค. ๋๋ FastAPI๋ฅผ ์ฌ์ฉ์ค์ด๋ผ์ ์๋ฒ์์ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ๊ฒฝ๋ก๋ฅผ ์ถ๊ฐํด์คฌ๋ค.
@app.get("/firebase-messaging-sw.js")
def fcm():
return FileResponse("firebase-messaging-sw.js")
- service worker ํ์ผ์์๋
importScripts
๋ฅผ ํ์ฉํด์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ถ์ฌ์ค ์ ์๋ค.
// firebase-messaging-sw.js
importScripts(
"https://www.gstatic.com/firebasejs/10.11.0/firebase-app-compat.js"
);
importScripts(
"https://www.gstatic.com/firebasejs/10.11.0/firebase-messaging-compat.js"
);
- ์ดํ๋ก๋ ๋ค๋ฅธ ๊ฑด ๋ณ๋ก ์๋ค. ๋ฉ์ธ js ํ์ผ์์ ์ค์ ํด์คฌ๋๋๋ก ํด์ฃผ๋ฉด ๋๋ค.
// firebase-messaging-sw.js
firebase.initializeApp({
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID",
measurementId: "YOUR_MEASUREMENT_ID",
});
const messaging = firebase.messaging();
https://medium.com/@cizu64/sending-notification-message-with-firebase-with-javascript-5d5c4bb02206
6. ๋ฉ์ธ์ง ์์ ์ค์
- ๋ฉ์ธ์ง ์์ ์ค์ ์๋
onMessage
์onBackgroundMessage
๋ ๊ฐ์ง๊ฐ ์๋ค. onMessage
: Foreground, focus๊ฐ ํด๋น ์๋น์ค์ ๊ฐ์๋ ๊ฒฝ์ฐ(์ฐฝ์ด ๋ ์์ ๋) ๋ฐ์ ๋ฉ์ธ์ง ์ฒ๋ฆฌ- ๋ฉ์ธ js์์ ์ฒ๋ฆฌํด์ผํ๋ค.
onBackgroundMessage
: Background, ํด๋น ์๋น์ค๋ฅผ ๋ฒ์ด๋์๋ ๊ฒฝ์ฐ์ ๋ฐ์ ๋ฉ์ธ์ง ์ฒ๋ฆฌ- service worker์์ ์ฒ๋ฆฌํด์ผํ๋ค.
! ์ ์ํด์ผํ๋ ํ์ผ ์ธ์์ ์ ์ํ๋ฉด ์ค๋ฅ ๋ฐ์ํ๋ ์ฃผ์
// index.js
onMessage(messaging, (payLoad) => {
console.log("Message Received");
console.log(payLoad);
var notificationTitle = payLoad.notification.title;
var notificationOptions = {
body: payLoad.notification.body,
icon: payLoad.notification.icon,
};
var notification = new Notification(notificationTitle, notificationOptions);
});
// firebase-messaging-sw.js
messaging.onBackgroundMessage(function (payload) {
console.log(
"[firebase-messaging-sw.js] Received background message ",
payload
);
const notificationTitle = payLoad.notification.title;
const notificationOptions = {
body: payLoad.notification.body,
icon: payLoad.notification.icon,
};
return self.registration.showNotification(
notificationTitle,
notificationOptions
);
});
7. Firebase console์์ ํ ์คํธ ๋ฉ์ธ์ง ๋ณด๋ด๊ธฐ
- ์ฒซ๋ฒ์งธ ์บ ํ์ธ ๋ง๋ค๊ธฐ - firebase ์๋ฆผ ๋ฉ์ธ์ง ์ ํ
- ๋๋ต์ ์ธ ๋ด์ฉ ์์ฑ ํ ํ ์คํธ ๋ฉ์ธ์ง ์ ์ก ํด๋ฆญ
- ์์์ ์์ฑํ ๋ด์ฉ์์
getToken
์ ํตํด์ ๋ฐ์ token์ FCM ๋ฑ๋ก ํ ํฐ ์ถ๊ฐ ๋์ ๋ณต๋ถ
- ํ ์คํธ ํด๋ฆญ
- ๋ฉ์ธ์ง ๋์ฐฉ ํ์ธ!!
- ์ฐฝ์ ๋ค๋ฅธ ํญ์ผ๋ก ๋๋ ค๋๊ณ background์์๋ ์ ๋์ฐฉํ๋์ง ํ์ธํ๊ธฐ
0. ๋ง์ฃผ์น ์ค๋ฅ ๋ชฉ๋ก
FirebaseError: Messaging: A problem occurred while unsubscribing the user from FCM: Requested entity was not found.
- chrome์์๋ง ๋ฐ์ํ๋?
- ์๋นํ ๊ณ ์ง์ ์ธ ๋ฌธ์ ๋ก ํ์ ๋จ.
title: "firebase.messaging.getToken() edgecase when push notification is set back to default (ask) · Issue #2364 · firebase/firebase-js-sdk"
image: "https://opengraph.githubassets.com/03116c0e7b563d83b2956dd4dd240aafe3681c25a9141c528f6dbf89d1dbfd6c/firebase/firebase-js-sdk/issues/2364"
description: "[REQUIRED] Describe your environment Operating System version: macOS Mojave Version 10.14.6 Browser version: Google Chrome Version 78.0.3904.97 Firebase SDK version: 7.0.0 Firebase Product: Firebas…"
url: "https://github.com/firebase/firebase-js-sdk/issues/2364"
- ์๋ฆผ ๊ถํ ์ค์ ์ ์ด๊ธฐํํ๋ฉด ํด๊ฒฐ๋๊ธด ํจ
messaging.setbackgroundmessagehandler is not a function
setBackgroundMessageHandler
๋onBackgroundMessage
๋ก ๋ณ๊ฒฝ๋จ
firebase messaging domexception failed to execute 'atob' on 'window' the string to be decoded is not correctly encoded
- VAPID ์ ๋๋ก ๋ฃ์ ๊ฒ ๋ง๋์ง ํ์ธ
messaging request permission is not a function
- firebase messaging ์
requestPermission
์Notification.requestPermission
์ผ๋ก ๋์ฒด๋จ
uncaught syntaxerror: unexpected token 'export' (at firebase-app.js:2520:1)
- script๋ฅผ module๋ก ์ ์ฉํด์ผ๋ง ์ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์์.
- module์ด ์๋๋ผ ์ผ๋ฐ์ ์ผ๋ก ์ ์ฉํ๊ณ ์ถ์ผ๋ฉด
firebase-app-combat.js
์ฌ์ฉ ๊ฐ๋ฅ- ๊ทผ๋ฐ ์ ๋ ์ ์๋์ต๋๋ค...
firebase-messaging-sw.js:1 uncaught domexception: failed to execute 'importscripts' on 'workerglobalscope'
- firebase serviceworker๋ ๋ฐ๋ก ๋ฑ๋กํ๋ ์ฝ๋๋ฅผ ๋ฃ์ง ์๋๋ค.
the script resource is behind a redirect, which is disallowed.
hostname/firebase-messaging-sw.js
๊ฒฝ๋ก๋ก ํ์ผ์ ์ ๊ทผ ๊ฐ๋ฅํ์ง ํ์ธ
728x90