2017-04-26 96 views
0

只要一个人来到我的网站,他们都面临着......如何通过特定操作触发浏览器通知请求?

enter image description here

但我只希望在用户点击“推”,使用户可以理解的背景下出现的弹出的请求。

enter image description here

如何只触发在用户点击“推送”浏览器通知请求?

我实施推送通过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"); 
    } 
    }); 
} 

回答

2

距离的application.js与功能总结你的通知,要求部分。 然后添加该功能来单击按钮的处理程序。

const askForNotification =() => { 
    //you code here 
    } 

    pushButton.addEventListener("click", askForNotification)