Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#89 feat add realtime notification with fcm #161

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 90 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"axios": "^1.6.2",
"date-fns": "^3.1.0",
"dotenv": "^16.3.1",
"firebase": "^9.23.0",
"framer-motion": "^10.16.16",
"jwt-decode": "^4.0.0",
"react": "^18.2.0",
Expand Down
28 changes: 28 additions & 0 deletions public/firebase-messaging-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
self.addEventListener('install', function () {
console.log('[FCM SW] 설치중..');
self.skipWaiting();
});

self.addEventListener('activate', function () {
console.log('[FCM SW] 실행중..');
});

self.addEventListener('push', function (e) {
if (!e.data.json()) return;

const resultData = e.data.json().notification;
// 필수! 알람 제목
const notificationTitle = resultData.title;
const notificationOptions = {
//필수! 알람 설명
body: resultData.body,
//필수! 알람 이미지(프로필, 로고)
icon: resultData.image,
//필수! 알람 분류태그 (전체, 투표, 나가기...)
tag: resultData.tag,
...resultData,
};
console.log('출력');

self.registration.showNotification(notificationTitle, notificationOptions);
});
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
import {Suspense} from 'react';
import {CookiesProvider} from 'react-cookie';
import {BrowserRouter} from 'react-router-dom';
import './firebase/messaging-init-in-sw';

import './sass/index.scss';

import MainRouter from './routes/MainRouter/MainRouter';
window.Kakao.init(import.meta.env.VITE_KAKAO_KEY);

const queryClient = new QueryClient();
function App() {
Expand Down
11 changes: 11 additions & 0 deletions src/api/invite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import axios from 'axios';

import {InviteCodeRequestParams} from '@/types/Invitation';

export const postJoin = async (spaceId: number) =>
await axios.post(`/api/auth/join/spaces/${spaceId}`, {withCredentials: true});
export const postJoinSpaces = async (params: InviteCodeRequestParams) => {
const {spaceId} = params;
const response = await axios.post(`/api/auth/join/spaces/${spaceId}/code`, {withCredentials: true});
return response.data.data;
};
21 changes: 21 additions & 0 deletions src/api/notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import axios from 'axios';

import {Token} from '@/types/notification';

export const sendNotificationToken = async (token: Token) => {
try {
const response = await axios.post('/api/notifications/token', token, {withCredentials: true});
return response.data;
} catch (error) {
console.error('[notification]토큰 전송 요청에 실패했습니다', error);
}
};

export const GetNotification = async () => {
try {
const response = await axios.get('/api/notifications');
return response.data.data;
} catch (error) {
console.log('[notification]알림내용을 가져오지 못했습니다');
}
};
6 changes: 6 additions & 0 deletions src/api/spaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import axios from 'axios';

export const spacesRequest = {
getSpaces: () => axios.get('/api/spaces').then((response) => response.data.data.spaces),
postSpaces: () => axios.post('/api/spaces'),
};
5 changes: 5 additions & 0 deletions src/api/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import axios from 'axios';

export const memberRequest = {
getMyInfo: () => axios.get('/api/members/my-info').then((response) => response.data.data),
};
Binary file added src/assets/alarm/alarmBlackBell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/alarm/alarmWhiteBell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
40 changes: 24 additions & 16 deletions src/components/Alarm/Alarm.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Slide } from "@chakra-ui/react";
import { useEffect, useState } from "react";
import {Slide} from '@chakra-ui/react';
import {useEffect, useState} from 'react';

import styles from "./Alarm.module.scss";
import styles from './Alarm.module.scss';

import useLockBodyScroll from "@/hooks/useLockBodyScroll";
import useLockBodyScroll from '@/hooks/useLockBodyScroll';

import Back from "@/components/Alarm/Back/Back";
import TabCapsule from "@/components/Alarm/TabCapsule/TabCapsule";
import Back from '@/components/Alarm/Back/Back';
import TabCapsule from '@/components/Alarm/TabCapsule/TabCapsule';

import { AlarmProps } from "@/types/alarm";
import {AlarmProps} from '@/types/alarm';

function Alarm({ isAlarmOpen, alarmClose }: AlarmProps) {
function Alarm({isAlarmOpen, alarmClose}: AlarmProps) {
// 알림 스타일링
const [isVisible, setIsVisible] = useState(isAlarmOpen);
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
Expand All @@ -30,34 +30,42 @@ function Alarm({ isAlarmOpen, alarmClose }: AlarmProps) {
};
}, [isAlarmOpen]);
const containerStyle = {
display: isVisible ? "block" : "none",
display: isVisible ? 'block' : 'none',
};
useEffect(() => {
const handleResize = () => {
setWindowWidth(window.innerWidth);
};
window.addEventListener("resize", handleResize);
window.addEventListener('resize', handleResize);
handleResize();
return () => {
window.removeEventListener("resize", handleResize);
window.removeEventListener('resize', handleResize);
};
}, []);
useLockBodyScroll(isAlarmOpen);

// 새로운 알림 상태관리
useEffect(() => {
if (isAlarmOpen === true) {
localStorage.removeItem('news');
console.log('비움');
}
}, [isAlarmOpen]);

return (
<div style={containerStyle} className={styles.page}>
<Slide
in={isAlarmOpen}
direction="right"
direction='right'
className={styles.slide}
style={{
width: windowWidth <= 450 ? "100%" : "45rem",
position: "absolute",
width: windowWidth <= 450 ? '100%' : '45rem',
position: 'absolute',
zIndex: 10,
}}
transition={{
exit: { duration: 0.5 },
enter: { duration: 0.5 },
exit: {duration: 0.5},
enter: {duration: 0.5},
}}
>
<Back alarmClose={alarmClose} />
Expand Down
Loading