Skip to content

Commit

Permalink
feat: fcm 기능 도입중
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-hak committed May 29, 2024
1 parent 4ef3369 commit 7a60223
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/components/pwa/Fcm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { initializeApp } from 'firebase/app';
import { getMessaging, getToken } from 'firebase/messaging';

const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_ID,
appId: process.env.NEXT_PUBLIC_APP_ID
};

const app = initializeApp(firebaseConfig);

/**
* FCM 토큰 발급
*/
export const setTokenHandler = async () => {
const messaging = getMessaging(app);
await getToken(messaging, {
vapidKey: process.env.NEXT_PUBLIC_VAPID_KEY
})
.then(async (currentToken) => {
if (!currentToken) {
// 토큰 생성 불가시 처리할 내용, 주로 브라우저 푸시 허용이 안된 경우에 해당한다.
} else {
// 토큰을 받았다면 여기서 DB에 저장하면 됩니다.
}
})
.catch((error) => {
console.error(error);
});
};

export const clickPushHandler = () => {
Notification.requestPermission().then((permission) => {
if (permission !== 'granted') {
// 푸시 거부됐을 때 처리할 내용
console.log('푸시 거부됨');
} else {
// 푸시 승인됐을 때 처리할 내용
console.log('푸시 승인됨');
}
});
};

const VAPID_KEY = process.env.NEXT_PUBLIC_VAPID_KEY;

export const getTokenHandler = async () => {
const messaging = getMessaging(app);
return await getToken(messaging, {
vapidKey: VAPID_KEY
})
.then(async (currentToken) => {
if (!currentToken) {
// 토큰 생성 불가시 처리할 내용, 주로 브라우저 푸시 허용이 안된 경우에 해당한다.
console.error('토큰 생성 불가');
} else {
// 토큰을 받았다면 여기서 supabase 테이블의 저장하면 됩니다.
console.log('currentToken', currentToken);
return currentToken;
}
})
.catch((error) => {
console.error('token error', error);
});
};
11 changes: 11 additions & 0 deletions src/components/pwa/Push.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const clickPushHandler = () => {
Notification.requestPermission().then((permission) => {
if (permission !== 'granted') {
// 푸시 거부됐을 때 처리할 내용
console.log('푸시 거부됨');
} else {
// 푸시 승인됐을 때 처리할 내용
console.log('푸시 승인됨');
}
});
};
28 changes: 28 additions & 0 deletions src/components/pwa/PushSend.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import admin, { ServiceAccount } from 'firebase-admin';

interface NotificationData {
data: {
title: string;
body: string;
image: string;
click_action: string;
};
token: string;
}
export const sendFCMNotification = async (data: NotificationData) => {
const serviceAccount: ServiceAccount = {
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
privateKey: process.env.NEXT_PUBLIC_FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
clientEmail: process.env.NEXT_PUBLIC_FIREBASE_CLIENT_EMAIL
};

if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
}

const res = await admin.messaging().send(data);

return res;
};
41 changes: 41 additions & 0 deletions src/firebase/firebasedb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app';
// import { getAnalytics } from "firebase/analytics";
import { getMessaging, getToken } from 'firebase/messaging';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_ID,
appId: process.env.NEXT_PUBLIC_APP_ID
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
// const analytics = getAnalytics(app);

/**
* FCM 토큰 발급
*/
export const setTokenHandler = async () => {
const messaging = getMessaging(app);
await getToken(messaging, {
vapidKey: process.env.NEXT_PUBLIC_VAPID_KEY
})
.then(async (currentToken) => {
if (!currentToken) {
// 토큰 생성 불가시 처리할 내용, 주로 브라우저 푸시 허용이 안된 경우에 해당한다.
} else {
// 토큰을 받았다면 여기서 DB에 저장하면 됩니다.
}
})
.catch((error) => {
console.error(error);
});
};

0 comments on commit 7a60223

Please sign in to comment.