Php 如何通过单击按钮来推送web通知
所以我在练习web通知,我偶然发现了这个教程,我不太理解这里的所有代码,但我试图通过一个接一个的尝试来实现,但我的一个问题是如何按按钮来推web通知 以下是我的代码: index.htmlPhp 如何通过单击按钮来推送web通知,php,jquery,web,notifications,service-worker,Php,Jquery,Web,Notifications,Service Worker,所以我在练习web通知,我偶然发现了这个教程,我不太理解这里的所有代码,但我试图通过一个接一个的尝试来实现,但我的一个问题是如何按按钮来推web通知 以下是我的代码: index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta nam
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Push Codelab</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.2.1/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.2.1/material.min.js"></script>
<link rel="stylesheet" href="styles/index.css">
</head>
<body>
<header>
<h1>Push Codelab</h1>
</header>
<main>
<p>Welcome to the push messaging codelab. The button below needs to be
fixed to support subscribing to push.</p>
<p>
<button disabled class="js-push-btn mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect">
Enable Push Messaging
</button>
<button class="js-push-btn mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect" id="notify">
Enable Push Messaging
</button>
</p>
<section class="subscription-details js-subscription-details is-invisible">
<p>Once you've subscribed your user, you'd send their subscription to your
server to store in a database so that when you want to send a message
you can lookup the subscription and send a message to it.</p>
<p>To simplify things for this code lab copy the following details
into the <a href="https://web-push-codelab.glitch.me//">Push Companion
Site</a> and it'll send a push message for you, using the application
server keys on the site - so make sure they match.</p>
<pre><code class="js-subscription-json"></code></pre>
</section>
</main>
<script src="scripts/main.js"></script>
<script src="https://code.getmdl.io/1.2.1/material.min.js"></script>
</body>
</html>
如您所见,我添加了一个名为pushNotification()的新函数在单击通知按钮时使用,但它不会发送通知。有没有更简单的代码?我的大脑已经崩溃了
目前,我可以通过访问我的开发者模式>应用程序>服务人员来推送通知。但是我想通过我创建的按钮来获得它。我想我已经得到了答案,showNotification是我所需要的全部,对于有相同问题的任何人,请尝试这个。为我工作
self.addEventListener('push', function (event) {
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
var data = {};
if (event.data) {
data = event.data.json();
}
console.log('Notification Received:');
console.log(data);
try {
var title = data.title;
var message = data.message;
var url = data.data.url;
var icon = "images/ns-logo.png";
event.waitUntil(self.registration.showNotification(title, {
body: message,
icon: icon,
badge: icon,
data: url
}));
}
catch (err) {
console.log('Notification Processing Failed:', err);
}
});
通常,这个实现应该转到ServiceWorker js文件,这样任何推送到达都会显示给用户,而无需最终用户手动干预- 自我注册。showNotification(…)
确保您已经注册了sw.js,并允许权限通知浏览器 HTML
var btnnotify = document.querySelector('#ABCD');
btnnotify.addEventListener('click', function () {
navigator.serviceWorker.getRegistration().then(sw => {
let options = {
body: "Let's buy some Nendoroid!",
icon: "img/Sukusuku Hakutaku.png",
};
sw.showNotification("I want to buy a Nendoroid!", options);
});
});
notifyButton.addEventListener('click', function() {
const title = 'Simple Title';
const options = {
body: 'Simple piece of body text.\nSecond line of body text :)'
};
swRegistration.showNotification(title, options);
})
self.addEventListener('push', function (event) {
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
var data = {};
if (event.data) {
data = event.data.json();
}
console.log('Notification Received:');
console.log(data);
try {
var title = data.title;
var message = data.message;
var url = data.data.url;
var icon = "images/ns-logo.png";
event.waitUntil(self.registration.showNotification(title, {
body: message,
icon: icon,
badge: icon,
data: url
}));
}
catch (err) {
console.log('Notification Processing Failed:', err);
}
});
<button id="ABCD" class="btn btn-block">Notify</button>
var btnnotify = document.querySelector('#ABCD');
btnnotify.addEventListener('click', function () {
navigator.serviceWorker.getRegistration().then(sw => {
let options = {
body: "Let's buy some Nendoroid!",
icon: "img/Sukusuku Hakutaku.png",
};
sw.showNotification("I want to buy a Nendoroid!", options);
});
});