Web Push Notification:如何使用Web Push PHP库?

Web Push Notification: How to use Web Push PHP Library?

我想在我的网站上添加网络通知。我在 Google 上搜索并找到了一些关于它的教程。如这些教程中所述,我设法向访问者显示订阅框,我也可以存储他们的数据。

Main.js

'use strict';

const applicationServerPublicKey = 'BBw_opB12mBhg66Dc94m7pOlTTHb5oqFAafbhN-BNeazWk8woAcSeHdgbmQaroCYssUkqFfoHqEJyCKw';

const pushButton = document.querySelector('.js-push-btn');

let isSubscribed = false;
let swRegistration = null;

function urlB64ToUint8Array(base64String) {
  const padding = '='.repeat((4 - base64String.length % 4) % 4);
  const base64 = (base64String + padding)
    .replace(/\-/g, '+')
    .replace(/_/g, '/');

  const rawData = window.atob(base64);
  const outputArray = new Uint8Array(rawData.length);

  for (let i = 0; i < rawData.length; ++i) {
    outputArray[i] = rawData.charCodeAt(i);
  }
  return outputArray;
}



if ('serviceWorker' in navigator && 'PushManager' in window) {
  console.log('Service Worker and Push is supported');

  navigator.serviceWorker.register('sw.js')
  .then(function(swReg) {
    console.log('Service Worker is registered', swReg);

    swRegistration = swReg;
  })
  .catch(function(error) {
    console.error('Service Worker Error', error);
  });
} else {
  console.warn('Push messaging is not supported');
  pushButton.textContent = 'Push Not Supported';
}


function initialiseUI() {
  // Set the initial subscription value
  swRegistration.pushManager.getSubscription()
  .then(function(subscription) {
    isSubscribed = !(subscription === null);

    if (isSubscribed) {
      console.log('User IS subscribed.');
    } else {
      console.log('User is NOT subscribed.');
    }

    updateBtn();
  });
}

function updateBtn() {
  if (isSubscribed) {
    pushButton.textContent = 'Disable Push Messaging';
  } else {
    pushButton.textContent = 'Enable Push Messaging';
  }

  pushButton.disabled = false;
}

sw.js

'use strict';

self.addEventListener('push', function(event) {
  console.log('[Service Worker] Push Received.');
  console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);

  const title = 'Motoroids Lab';
  const options = { 
    body: 'Motoroids',
    icon: 'images/icon.png',
    badge: 'images/badge.png'
  };

  event.waitUntil(self.registration.showNotification(title, options));
});


self.addEventListener('notificationclick', function(event) {
  console.log('[Service Worker] Notification click Received.');

  event.notification.close();

  event.waitUntil(
    clients.openWindow('https://developers.google.com/web/')
  );
});

但现在我卡住了。无论我多么努力,都很难理解如何从我的服务器发送推送消息:(

我在我的服务器上启用了 SSL。我使用 composer require minishlink/web-push 命令在服务器上安装了 PHP Web 推送库及其依赖项。

但下一步是什么?我也看不懂他们的文档。 https://github.com/web-push-libs/web-push-php

我需要一些帮助。请帮助我了解它是如何工作的以及如何使用它。

谢谢

看看https://web-push-book.gauntface.com/ for a general introduction to Web Push. The Chapter How Push Works and Subscribing a User你应该特别感兴趣。总结:

  • 在客户端,您需要通过调用 pushManager.subscribe 创建订阅。更多信息 here.
  • 您应该将此订阅发送到您的服务器。例如,发出 AJAX 请求以将订阅(端点、keys.p256dh、keys.auth)发送到服务器。更多信息 here.
  • 在服务器上,您使用 PHP Web 推送库发送推送消息。首先,您需要使用密钥对配置此库。然后,您需要使用订阅(端点,keys.p256dh,keys.auth)发送推送消息。更多信息 here.

您必须将此代码添加到您的项目中https://developers.google.com/web/fundamentals/getting-started/codelabs/push-notifications/有时您必须将代码添加到您已经创建的部分