Web推送通知:如何使用Web Push PHP库?

问题描述 投票:11回答:2

我想将Web通知添加到我的网站。我在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 Push库及其依赖项。

但下一步是什么?我也无法理解他们的文档。 https://github.com/web-push-libs/web-push-php

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

谢谢

php browser push-notification web-push
2个回答
13
投票

请查看https://web-push-book.gauntface.com/,了解Web Push的一般介绍。章节How Push WorksSubscribing a User对你来说应该特别有趣。综上所述:

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

-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.