2017-04-24 77 views
-1

...如何使用JavaScript的点击当用户点击触发POST

<%= content_tag(:button, "Send", class: "webpush-button") %> 
# Previous Attempt: <%= button_to 'send', class: "webpush-button" %> 

<script> 
    $('.webpush-button').on('click', (e) => { 
    navigator.serviceWorker.ready 
    .then((serviceWorkerRegistration) => { 
     serviceWorkerRegistration.pushManager.getSubscription() 
     .then((subscription) => { 
     $.post("/post", { 
      subscription: subscription.toJSON(), 
      message: 'You clicked a button!' 
     }); 
     }); 
    }); 
    }); 
</script> 

他应该通过采取...

class PushNotificationsController < ApplicationController 
    def push 
    Webpush.payload_send(
     message: params[:message], 
     endpoint: params[:subscription][:endpoint], 
     p256dh: params[:subscription][:keys][:p256dh], 
     auth: params[:subscription][:keys][:auth], 
     vapid: { 
     subject: "mailto:[email protected]", 
     public_key: ENV['VAPID_PUBLIC_KEY'], 
     private_key: ENV['VAPID_PRIVATE_KEY'] 
     } 
    ) 
    end 
end 

而是没有任何反应。该.webpush-button的JavaScript从来没有踢。我把它放在两个地方,它仍然没有效果...

的application.js

/ Register the serviceWorker script at /serviceworker.js from our server if supported 
if (navigator.serviceWorker) { 
    navigator.serviceWorker.register('/serviceworker.js').then(function(reg) { 
    console.log('Service worker change, registered the service worker'); 
    }); 
} 
// Otherwise, no push notifications :(
else { 
    console.error('Service worker is not supported in this browser'); 
} 

// When serviceWorker is supported, installed, and activated, 
// subscribe the pushManager property with the vapidPublicKey 
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => { 
    serviceWorkerRegistration.pushManager.subscribe({ 
    userVisibleOnly: true, 
    applicationServerKey: window.vapidPublicKey 
    }); 
}); 

$('.webpush-button').on('click', (e) => { 
    navigator.serviceWorker.ready 
    .then((serviceWorkerRegistration) => { 
    serviceWorkerRegistration.pushManager.getSubscription() 
    .then((subscription) => { 
     $.post('/push', { 
     subscription: subscription.toJSON(), 
     message: 'You clicked a button!' 
     }); 
    }); 
    }); 
}); 

// 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"); 
    } 
    }); 
} 

application.html.erb

<script> 
    window.vapidPublicKey = new Uint8Array("<%= @decodedVapidPublicKey %>"); 
</script> 

现在根据我使用的tutorialgit代码...订阅应该从serviceworker那么为什么我仍然得到一个零错误?

我使用serviceworker & webpush宝石和遵循这一VAPID tutorial

不是重复。另一个问题集中在params上。这一个是专注于JavaScript不触发。

+0

可能重复的[订阅参数无网络推送serviceworker?](http://stackoverflow.com/questions/43572177/subscription-params-nil-for-web-push-serviceworker) –

回答

1

首先,你只应该把JavaScript放在一个地方。

二,添加页面后.webpush-button click方法是ready

$(document).on('ready page:load', function() { 

}); 

而在Chrome浏览器开发工具,标签Event Listeners,检查HTML元素具有Click事件。

+0

是的,它确实有点击事件'button.webpush-button'。随着我从你的答案做出的调整,JavaScript仍然不会触发。我也从application.js –

+0

删除了重复。你也可以在你的方法之前添加'console.log',如果它显示日志,那么javascript是''触发的',只是你的代码逻辑是错误的。 –