Я создаю сервис, который отправляет веб-push-уведомления, с php и firebase.уведомление работает хорошо, но я не могу добавить ссылку для отправки пользователя на другой сайт.запрос сделан с curl php.this event.data ||event.data.notification всегда null.
self.addEventListener('notificationclick', function (event) {
let nextUrl = event.notification.data.click_action; // ALWAYS NULL
console.log('[Service Worker] Notification click Received.', nextUrl);
event.notification.close();
event.waitUntil(
clients.openWindow(nextUrl)
);
});
здесь весь код .. firebase-messaging-sw.js
// [START initialize_firebase_in_sw]
importScripts('https://www.gstatic.com/firebasejs/5.5.9/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.5.9/firebase-messaging.js');
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': '***'
});
// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]
messaging.setBackgroundMessageHandler(function (payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = payload.data.title;
const notificationOptions = {
body: payload.data.body,
icon: payload.data.icon,
image: payload.data.image,
click_action: payload.data.nextLink
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
// [END background_handler]
self.addEventListener('notificationclick', function (event) {
let nextUrl = event.notification.data.click_action; // ALWAYS NULL
console.log('[Service Worker] Notification click Received.', nextUrl);
event.notification.close();
event.waitUntil(
clients.openWindow(nextUrl)
);
});
self.addEventListener('install', event => {
self.skipWaiting();
});
main.js
// Initialize Firebase
var config = {
apiKey: "***",
authDomain: "***",
databaseURL: "***",
projectId: "***",
storageBucket: "***",
messagingSenderId: "***"
};
firebase.initializeApp(config);
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
let newUser = {};
let isSubscribed = false; // TODO
if ('serviceWorker' in navigator && 'PushManager' in window) {
messaging.requestPermission().then(function () {
return messaging.getToken();
}).then(function (token) {
window.localStorage.setItem('token', token); // TODO
newUser = {
endPoint: token,
dateClicked: new Date().toLocaleDateString(),
hourClicked: new Date().toLocaleTimeString()
}
if (!checkIfExist()) {
$.ajax('***' + token + '.json', {
accept: "application/json",
method: 'PUT',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(newUser),
error: function (err) {
console.log('Unable to save user in DB.', err);
}
});
} else {
console.log("User Already Exist");
}
})
.catch(function (err) {
console.log('Unable to get permission to notify.', err);
});
}
function checkIfExist(token) {
return window.localStorage.getItem('token') === token;
}
// ForeGround
// messaging.onMessage(function (payload) {
// console.log("Message Received");
// notificationTitle = payload.data.title;
// notificationOptions = {
// body: payload.data.body,
// icon: payload.data.icon
// };
// var notification = new Notification(notificationTitle, notificationOptions);
// console.log(notification);
// });