doubingjiu3199 2019-06-24 09:53
浏览 55

推送事件未被触发

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow