I've downloaded this https://github.com/Minishlink/web-push-php-example. There is a send notification button in it which fetch
a php file which pushes a notification using webpush https://github.com/web-push-libs/web-push-php.
It was working for a while and then just stopped working. After it stopped working I replaced with a working copy I had and still didn't show the notification.
The response from fetch
to send_push_notification.php
shows
[v] Message sent successfully for subscription https://fcm.googleapis.com/wp/f6ST-d7Ek3o:APA91bH3ZaT_QYo...
but I don't see the push notification. I have a console.log
in push
event handler in service-worker and I don't see the push notification
The js which calls the fetch
to php file(send_push_notification.php
)
sendPushButton.addEventListener('click', () =>
navigator.serviceWorker.ready
.then(serviceWorkerRegistration => serviceWorkerRegistration.pushManager.getSubscription())
.then(subscription => {
const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0];
const jsonSubscription = subscription.toJSON();
fetch('send_push_notification.php', {
method: 'POST',
body: JSON.stringify(Object.assign(jsonSubscription, { contentEncoding })),
});
})
);
The service-worker file:
self.addEventListener('push', function (event) {
console.log("PUSHRECEived");
const sendNotification = body => {
return self.registration.showNotification(title, {
body,
});
};
if (event.data) {
const message = event.data.text();
event.waitUntil(sendNotification(message));
}
});
Edit It works after I restart my PC. What could be the reason.