Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web push notifications

Web push notifications

Diego Gurgel

July 02, 2017
Tweet

More Decks by Diego Gurgel

Other Decks in Programming

Transcript

  1. if ('serviceWorker' in navigator) { navigator.serviceWorker.register('sw.js') .then(initialState); }else { console.log(':(');

    } navigator.serviceWorker.ready.then(swRegistration=>{ return swRegistration.pushManager .subscribe({userVisibleOnly:true}); }).then(subscription=>{ console.log(subscription); /*Enviar para o servidor*/ }); ☁ "gcm_sender_id": "827747435376" Notification.requestPermission().then(status)
  2. const webpush = require('web-push'); const vapidKeys = webpush.generateVAPIDKeys(); webpush.setGCMAPIKey('GCMKEY'); webpush.setVapidDetails(

    'mailto:[email protected]', vapidKeys.publicKey, vapidKeys.privateKey ); const pushSubscription = { endpoint:”https://android.googleapis.com/gcm/send/ewcFEKglM…”, keys:{ p256dh:”BERT29SHqILHUjCaeN8NJMRXL8oQSQs…”, auth:"gzOk5sX1HTtfXb4PmVoiFA==" } } ☁ const payload = { title: 'Frontend meetup', message: ':) ', };
  3. ⚙ 'use strict'; self.addEventListener('push', function(event) { let data = event.data.json();

    event.waitUntil( self.registration.showNotification(data.title, { body: data.message, }) ); });