Skip to content

Commit

Permalink
Merge branch 'develop' into stickers
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanNaum committed May 27, 2024
2 parents 1cb4e1b + 2298272 commit 479ca8c
Show file tree
Hide file tree
Showing 7 changed files with 1,764 additions and 81 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/Components/*/*.precompiled.js
src/Components/*/*.precompiled.js
src/firebase-messaging-sw.js
1,720 changes: 1,641 additions & 79 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"proxy": "http://localhost:8080",
"devDependencies": {
"@parcel/transformer-sass": "^2.12.0",
"buffer": "^6.0.3",
"cookie-parser": "^1.4.6",
"cssnano": "^7.0.1",
"eslint": "^8.57.0",
Expand All @@ -40,9 +41,12 @@
"parcel": "^2.12.0",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"process": "^0.11.10",
"sass": "^1.75.0"
},
"dependencies": {
"@parcel/watcher": "^2.4.1",
"firebase": "^10.12.1",
"handlebars": "^4.7.8"
}
}
3 changes: 2 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
<script src="./Components/Search/Search.precompiled.js"></script>
<script src="./Components/ChatInput/ChatInput.precompiled.js"></script>
<script src="./Components/Sticker/Sticker.precompiled.js"></script>

<script src="./index.js" type="module"></script>
<script src="./utils/router.js" type="module"></script>
<script type="text/javascript" src="https://www.gstatic.com/firebasejs/3.6.8/firebase.js"></script>
</body>

</html>
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { AuthAPI } from './utils/API/AuthAPI.js';
import './Components/Form/Form.precompiled.js';
import './Components/Chat/Chat.precompiled.js';
import './Components/Contacts/Contacts.precompiled.js';
import './Components/ChatList/ChatList.precompiled.js';
import './Components/ContactItem/ContactItem.precompiled.js';
import './Components/ChatListItem/ChatListItem.precompiled.js';
import './Components/Message/Message.precompiled.js';
import './Components/Profile/Profile.precompiled.js';
import './Components/Search/Search.precompiled.js';
import './Components/ChatInput/ChatInput.precompiled.js';
import './utils/router.js';
import './utils/notifications.js';

function registerServiceWorker() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.getRegistrations()
.then(function (registrations) {
console.log(registrations);
if (registrations.length === 0) {
return navigator.serviceWorker.register(
new URL('./serviceWorker.js', import.meta.url),
Expand Down
26 changes: 26 additions & 0 deletions src/utils/API/FirebaseAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { makeBaseRequest } from './common.js';
import { baseUrl, protocol } from './config.js';

/**
* API для работы с уведомлениями
* @class FirebaseAPI
*/
export class FirebaseAPI {
async setToken(token) {
try {
return makeBaseRequest(
`${protocol}://${baseUrl}/setFirebaseToken`,
'POST',
{
token: token,
},
);
} catch (error) {
console.error(
'There was a problem with the fetch operation:',
error,
);
throw error;
}
}
}
76 changes: 76 additions & 0 deletions src/utils/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { initializeApp } from 'firebase/app';
import { getMessaging, getToken, onMessage } from 'firebase/messaging';
import { FirebaseAPI } from './API/FirebaseAPI.js';

// Ваши Firebase конфигурации
const firebaseConfig = {
apiKey: 'AIzaSyAxLahuF_2pMyI_XDNeaGw7FvoojnLZor0',
authDomain: 'chatme-45ce9.firebaseapp.com',
projectId: 'chatme-45ce9',
storageBucket: 'chatme-45ce9.appspot.com',
messagingSenderId: '635967212150',
appId: '1:635967212150:web:f05baac8634df1bd0fed57',
};

// Инициализация Firebase
const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);

if ('Notification' in window) {
Notification.requestPermission().then((permission) => {
if (permission === 'granted') {
subscribe();
}
});
if (window.Notification.permission === 'granted') {
onMessage(messaging, function (payload) {
new Notification(payload.notification.title, payload.notification);
});
}
}

function subscribe() {
getToken(messaging, {
vapidKey:
'BAjW0yRMyBXErSXFcv8N4eBm5ZWDfqV0RrP-jXMl0fNWZPstuPt3QtjmpiB1GAz5j1olmZPEdCBwpUbDWAh_Q8I',
})
.then((currentToken) => {
if (currentToken) {
sendTokenToServer(currentToken);
} else {
console.warn('Не удалось получить токен.');
setTokenSentToServer(false);
}
})
.catch((err) => {
console.warn('При получении токена произошла ошибка.', err);
setTokenSentToServer(false);
});
}

function sendTokenToServer(currentToken) {
if (!isTokenSentToServer(currentToken)) {
console.log('Отправка токена на сервер...');

const firebaseApi = new FirebaseAPI();
firebaseApi.setToken(currentToken);

setTokenSentToServer(currentToken);
} else {
console.log('Токен уже отправлен на сервер.');
}
}

function isTokenSentToServer(currentToken) {
return (
window.localStorage.getItem('sentFirebaseMessagingToken') ===
currentToken
);
}

function setTokenSentToServer(currentToken) {
window.localStorage.setItem(
'sentFirebaseMessagingToken',
currentToken ? currentToken : '',
);
}

0 comments on commit 479ca8c

Please sign in to comment.