我想用php发送推送到网页(chrome),我使用https://github.com/Minishlink/web-push lib进行这项工作,但我不知道如何生成或提供$userPublicKey
和{{1} }。
$userAuthToken
答案 0 :(得分:0)
在客户端提供$ userPublicKey和$ userAuthToken,如下所示:
首先注册服务人员:
if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('sw.js').then(function() {
return navigator.serviceWorker.ready;
}).then(function(serviceWorkerRegistration) {
reg = serviceWorkerRegistration;
subscribeButton.disabled = false;
console.log('Service Worker is ready :^)', reg);
}).catch(function(error) {
console.log('Service Worker Error :^(', error);
});
}
为订阅按钮添加事件侦听器:
subscribeButton.addEventListener('click', function() {
if (isSubscribed) {
unsubscribe();
} else {
subscribe();
}
});
并在pushSubscription.getKey()中获取$ userPublicKey和$ userAuthToken,如下所示:
function subscribe() {
reg.pushManager.subscribe({userVisibleOnly: true}).
then(function(pushSubscription) {
sub = pushSubscription;
console.log(sub.getKey('auth'));
//userPublicKey
console.log(toBase64(sub.getKey('p256dh')));
//userAuthToken
console.log(toBase64(sub.getKey('auth')));
console.log('Subscribed! Endpoint:', sub.endpoint);
subscribeButton.textContent = 'Unsubscribe';
isSubscribed = true;
});
}