-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into stickers
- Loading branch information
Showing
7 changed files
with
1,764 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : '', | ||
); | ||
} |