This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vapidPublicKey = new Uint8Array(#{Base64.urlsafe_decode64($vapid_public).bytes}); | |
function checkNotifs(obj){ | |
if (!("Notification" in window)) { //1 | |
console.error("This browser does not support desktop notification"); | |
} | |
// Let's check whether notification permissions have already been granted | |
else if (Notification.permission === "granted") { //2 | |
console.log("Permission to receive notifications has been granted"); | |
getKeys(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gem install webpush |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gem install serviceworker-rails |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#In rails console | |
require 'webpush' | |
# One-time, on the server | |
vapid_key = Webpush.generate_key | |
# Save these in our application server settings | |
vapid_key.public_key | |
# => "BC1mp...HQ=" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/assets/javascript | |
{ | |
"name":"My App", | |
"short_name":"my-app", | |
"start_url":"/", | |
"icons":[ | |
{ | |
"src":"/images/my-push-logo-192x192.png", | |
"sizes":"192x192", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- index.html --> | |
<link rel="manifest" href="/manifest.json" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails g serviceworker:install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/initializers/serviceworker.rb | |
WebPushDemo::Application.configure do | |
config.serviceworker.routes.draw do | |
# map to assets implicitly | |
match "/serviceworker.js" | |
match "/manifest.json" | |
match "/logo.png" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/assets/javascript/serviceworker.js | |
self.addEventListener("push", function(event) { | |
var title = (event.data && event.data.text().split("-")[0]) || "Message recieved"; | |
var body; body = event.data.text().split("-")[1]; | |
var tag = "push-simple-demo-notification-tag"; | |
var icon = 'logo.png'; | |
event.waitUntil( | |
self.registration.showNotification(title, { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//app/views/content/index.html.erb | |
javascript: | |
var vapidPublicKey = new Uint8Array(#{Base64.urlsafe_decode64($vapid_public).bytes}); | |
function checkNotifs(obj){ | |
if (!("Notification" in window)) { //1 | |
console.error("This browser does not support desktop notification"); | |
} | |
// Let's check whether notification permissions have already been granted |
OlderNewer