一旦有人来到我的网站,他们就会遇到......
但是我只希望在用户点击&#34后推出该弹出框;推送"这样用户就可以理解请求的上下文。
如何在用户点击"推送"?
时触发浏览器通知请求我通过serviceworker gem,webpush gem,VAPID tutorial实施推送。
<%= form_for(@challenge) do |f| %>
<%= f.check_box :push %>
<label for="challenge_push">Push <%= image_tag "laptop.png" %></label>
<% end %>
的application.js
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
console.error("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
console.log("Permission to receive notifications has been granted");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
console.log("Permission to receive notifications has been granted");
}
});
}
答案 0 :(得分:2)
使用函数从application.js中发送通知请求部分。 然后添加该功能以单击按钮的处理程序。
const askForNotification = () => {
//you code here
}
pushButton.addEventListener("click", askForNotification)