2017-04-26 24 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)