Я пытаюсь реализовать FCM (Firebase Cloud Messaging) с использованием Play Framework.
Это мой сервис. Работник:
self.addEventListener('push', function (event) {
console.log('Received a push message', event);
var jsonObj = event.data.json();
var title= jsonObj.notification.title;
var bodyn= jsonObj.notification.body;
var icons= jsonObj.notification.icon;
console.log('Received a push message', title);
var notificationOptions = {
body: bodyn,
icon: icons,
tag: title,
data: null
};
if (self.registration.showNotification) {
self.registration.showNotification(title, notificationOptions);
return;
} else {
new Notification(title, notificationOptions);
}
});
В файле main.scala.html я написал этот код:
<script src="@routes.Assets.at("javascripts/firebase/firebase-app.js")"></script>
<script src="@routes.Assets.at("javascripts/firebase/firebase-messaging.js")"></script>
<script src="@routes.Assets.at("javascripts/firebase/firebase.js")"></script>
<script>
var config = {....};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.usePublicVapidKey("...........");
messaging.requestPermission()
.then(function () {
console.log('Notification permission granted.');
messaging.getToken()
.then(function (refreshedToken){
console.log('Token refreshed.');
sendTokenToServer(refreshedToken);
})
.catch(function (err) {
console.log('Unable to retrieve refreshed token',err);
});
})
.catch(function (err) {
console.log('Unable to get permission to notify.', err);
});
messaging.onMessage(function (data) {
console.log("ok");
const notificationTitle = data.notification.title;
const notificationOptions = {
body: data.notification.body,
icon: data.notification.icon,
};
});
Проблема, которую я получил из браузера:
"Не удалось зарегистрировать ServiceWorker: при получении сценария получен неверный код HTTP res… (404).
работник службы указан в главном файле, который выполняет первый в моем проекте.