Last active
January 19, 2017 10:33
-
-
Save miyucy/ac33be67ff42b2ad0da9356079bf329b to your computer and use it in GitHub Desktop.
This file contains 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
require 'web_push' | |
# Generate a keys and puts it to Rails.application.secrets.web_push_public_key | |
pkey = WebPush::Utils.generate_vapid_pkey | |
# Rails.application.secrets.web_push_private_key | |
vapid_private_key = Base64.urlsafe_encode64(pkey.private_key.to_bn.to_s(2)).delete('=') | |
puts "vapid_private_key = #{vapid_private_key}" | |
# Rails.application.secrets.web_push_public_key | |
vapid_public_key = Base64.urlsafe_encode64(pkey.public_key.to_bn.to_s(2)).delete('=') | |
puts "vapid_public_key = #{vapid_public_key}" |
This file contains 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
self.addEventListener('push', function (event) { | |
var json = event.data.json(); | |
self.registration.showNotification(json.title, { body: json.body, icon: json.icon }); | |
}); | |
self.addEventListener('notificationclick', function (event) { | |
event.notification.close(); | |
}, false); |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="x-ua-compatible" content="ie=edge"> | |
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1, maximum-scale=1"> | |
<script src="/push.js"></script> | |
</head> | |
<body> | |
<pre id="subscription"></pre> | |
</body> | |
</html> |
This file contains 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
function urlBase64ToUint8Array(base64String) { | |
var padding = '='.repeat((4 - base64String.length % 4) % 4); | |
var base64 = (base64String + padding).replace(/\-/g, '+').replace(/_/g, '/'); | |
var rawData = window.atob(base64); | |
var outputArray = new Uint8Array(rawData.length); | |
for (var i = 0, n = rawData.length; i < n; ++i) { | |
outputArray[i] = rawData.charCodeAt(i); | |
} | |
return outputArray; | |
} | |
document.addEventListener('DOMContentLoaded', function () { | |
if ('serviceWorker' in navigator) { | |
navigator.serviceWorker.getRegistration().then(function (registration) { | |
navigator.serviceWorker.register('/push-sw.js', { scope: '/' }).then(function (registration) { | |
navigator.serviceWorker.ready.then(function (sw) { | |
Notification.requestPermission(function (permission) { | |
if (permission !== 'denied') { | |
// sw.pushManager.getSubscription().then(function (subscription) { | |
// return subscription.unsubscribe(); | |
// }).then(function (successful) { | |
// console.log("successful:", successful); | |
// }).catch(console.error); | |
var applicationServerKey = urlBase64ToUint8Array('BMeQodK4woWkq1m-SvQzfaeNLnslX9NmbxEZLywYqPrZdW0SeLW28cRQ-D91sxey547aUjz26mXPND0jwXnZTj0'); | |
sw.pushManager.subscribe({ | |
userVisibleOnly: true, | |
applicationServerKey: applicationServerKey, | |
}).then(function (subscription) { | |
console.log('subscription.endpoint = ' + subscription.endpoint); | |
console.log('subscription.keys.p256dh = ' + subscription.getKey('p256dh')); | |
console.log('subscription.keys.auth = ' + subscription.getKey('auth')); | |
console.log(subscription.toJSON()); | |
var pre = document.getElementById('subscription'); | |
pre.textContent = JSON.stringify(subscription, null, ' '); | |
}).catch(function (error) { | |
console.error(error); | |
}); | |
} | |
}); | |
}); | |
}).catch(console.error); | |
}); | |
} | |
}); |
This file contains 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
require 'web_push' | |
subscription = JSON.parse <<JSON, symbolize_names: true | |
Paste from Firefox | |
JSON | |
wp = WebPush.new subscription | |
wp.set_vapid_details('mailto:fistfvck@gmail.com', | |
'BMeQodK4woWkq1m-SvQzfaeNLnslX9NmbxEZLywYqPrZdW0SeLW28cRQ-D91sxey547aUjz26mXPND0jwXnZTj0', | |
'_g3ShUCgTWNFEspwTfmkhHg3NTOnPQQYYAtjHYQxehY') | |
wp.send_notification( | |
{ | |
title: "LGTM", | |
body: "LGTM", | |
icon: "https://example.com/icon.jpg" | |
}.to_json | |
) | |
__END__ | |
∴ ruby push-prepare.rb | |
vapid_private_key = _g3ShUCgTWNFEspwTfmkhHg3NTOnPQQYYAtjHYQxehY | |
vapid_public_key = BMeQodK4woWkq1m-SvQzfaeNLnslX9NmbxEZLywYqPrZdW0SeLW28cRQ-D91sxey547aUjz26mXPND0jwXnZTj0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruby -run -e httpd -- open http://localhost:8080/push.html # Rewrite push.rb. (copy from firefox, and paste to push.rb) ruby push.rb