Я внедряю уведомления firebashe pu sh. Все работает нормально, когда вкладка клиента сфокусирована, но если она не в фокусе, я получаю всплывающее уведомление, но onMessage не запускается и содержимое страницы не обновляется.
В своих уведомлениях я хочу обновить небольшой текст, который я отправляю с моего сервера в «data» json компонент ответа firebase.
Как я уже сказал, он отлично работает, если вкладка сфокусирована, но если это не так, я просто получаю всплывающее уведомление и этот текст не обновляется
Вот мой "firebase-messaging-sw. js"
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js');
var firebaseConfig = {
apiKey: "*******************",
authDomain: "*******************",
databaseURL: "*******************",
projectId: "*******************",
storageBucket: "*******************-*******************.*******************.com",
messagingSenderId: "*******************",
appId: "1:*******************:*******************"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
Вот мой init. js (для инициализации firebase)
var firebaseConfig = {
apiKey: "*******************",
authDomain: "*******************",
databaseURL: "*******************",
projectId: "*******************",
storageBucket: "*******************-*******************.*******************.com",
messagingSenderId: "*******************",
appId: "1:*******************:*******************"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.usePublicVapidKey('********************************************');
Конечно, я включил библиотеки Firebase до
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-messaging.js"></script>
И вот моя страница js (с функцией "onMessage")
messaging.onMessage((payload) => {
var data = null;
if( typeof payload.data === 'undefined'){
data = payload.notification;
}else{
data = payload.data;
}
myFunction(data)/*Do something in my web, update a litle text*/
console.log('Message received. ', payload);
});
Надеюсь, вы могли бы помочь мне Спасибо Вам очень и очень жаль за мой плохой английский sh.