VAPID - Voluntary Application Server Identification
Version 0.02
use VAPID qw/generate/; my ($public, $private) = generate_vapid_keys() validate_public_key($public); validate_private_key($private); ... my $header = generate_vapid_header( 'https://updates.push.services.mozilla.com', 'mailto:email@lnation.org', $public, $private, time + 60 );
Generates vapid private and public keys.
Generates the Authorization and Crypto-Key headers that should be passed when making a request to push a notification.
Generates a time that is in future based upon the number of seconds if passed, the default is 12 hours.
Validate the subject.
Validate the public key.
Validate the private key.
Validate the expiration key.
The following is pseudo code but it should get you started.
my ($public, $private) = generate_vapid_keys() $c->stash({ VAPID_USER_PUBLIC_KEY => $public });
var publicKey = [% VAPID_USER_PUBLIC_KEY %]; navigator.serviceWorker.getRegistrations().then(function (registrations) { navigator.serviceWorker.register('/service-worker.js').then(function (worker) { console.log('Service Worker Registered'); worker.pushManager.getSubscription().then(function(sub) { if (sub === null) { // Update UI to ask user to register for Push subscribeUser(); console.log('Not subscribed to push service!'); } else { // We have a subscription, update the database console.log('Subscription object: ', sub); } }); }); }); function subscribeUser() { if ('serviceWorker' in navigator) { navigator.serviceWorker.ready.then(function(reg) { reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: publicKey }).then(function(sub) { // We have a subscription, update the database console.log('Endpoint URL: ', sub.endpoint); }).catch(function(e) { if (Notification.permission === 'denied') { console.warn('Permission for notifications was denied'); } else { console.error('Unable to subscribe to push', e); } }); }) } }
self.addEventListener('push', function(e) { var body; if (e.data) { body = e.data.text(); } else { body = 'Push message no payload'; } var options = { body: body, icon: 'images/notification-flat.png', vibrate: [100, 50, 100], data: { dateOfArrival: Date.now(), primaryKey: 1 }, }; e.waitUntil( self.registration.showNotification('Push Notification', options) ); });
{ "short_name" : "Push", "name" : "Push Dashboard", "icons" : [ { "src" : "/icon-144x144.png", "type" : "image/png", "sizes" : "144x144" } ], "display" : "standalone", "start_url" : "/", "background_color" : "#fff", "theme_color" : "#fff", "scope" : "/" }
my $notificaiton_host = URI->new($subscription_url)->host; my $header = generate_vapid_header( "https://$notification_host", 'mailto:email@lnation.org', $public, $private, time + 60 );
curl "{SUBSCRIBE_URL}" --request POST --header "TTL: 60" --header "Content-Length: 0" --header "Authorization: {AUTHORIZATION_HEADER}" --header "Crypto-Key: {CRYPTO_KEY_HEADER}"
LNATION, <email at lnation.org>
<email at lnation.org>
Please report any bugs or feature requests to bug-vapid at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=VAPID. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-vapid at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc VAPID
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
https://rt.cpan.org/NoAuth/Bugs.html?Dist=VAPID
CPAN Ratings
https://cpanratings.perl.org/d/VAPID
Search CPAN
https://metacpan.org/release/VAPID
This software is Copyright (c) 2020 by LNATION.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install VAPID, copy and paste the appropriate command in to your terminal.
cpanm
cpanm VAPID
CPAN shell
perl -MCPAN -e shell install VAPID
For more information on module installation, please visit the detailed CPAN module installation guide.