diff --git a/scripts/SettingsController.js b/scripts/SettingsController.js index d3bb5b9..b74f1a1 100644 --- a/scripts/SettingsController.js +++ b/scripts/SettingsController.js @@ -11,7 +11,7 @@ angular.module('flashy.SettingsController', ['ui.router']). console.log('chrome'); console.log('executing things outside of module'); - var PUSH_SERVER_URL = '/app/subscribe/'; + var PUSH_SERVER_URL = '/api/subscribe/'; function onPushSubscription(pushSubscription) { console.log('pushSubscription = ', pushSubscription.endpoint); @@ -20,37 +20,11 @@ angular.module('flashy.SettingsController', ['ui.router']). // In this demo we just use send these values to // our server via XHR which sends a push message. - console.log('pushSubscription: ', pushSubscription); - - // Code to handle the XHR - var formData = new FormData(); - var endpoint = pushSubscription.endpoint; + var subscriptionId = pushSubscription.subscriptionId; - if ('subscriptionId' in pushSubscription) { - // Make the endpoint always contain the subscriptionId - // so the server is always consistent - if (!endpoint.includes(pushSubscription.subscriptionId)) { - endpoint += '/' + pushSubscription.subscriptionId; - } - } - - formData.append('registration_id', endpoint); - console.log('registration_id: ', endpoint); - - console.log('tryna push'); - - var req = new XMLHttpRequest(); - req.addEventListener('load', function(response) { - console.log('response: ', response); - }); - req.addEventListener('error', function(error) { - console.log('error: ', error); - }); - req.open('POST', PUSH_SERVER_URL); - req.send(formData); - console.log('pushed?'); - + console.log('registration_id: ', subscriptionId); + $http.post(PUSH_SERVER_URL, {'registration_id': subscriptionId}); } function subscribeDevice() { @@ -238,7 +212,7 @@ angular.module('flashy.SettingsController', ['ui.router']). }); } -// Once the service worker is registered set the initial state + // Once the service worker is registered set the initial state function initialiseState() { // Is the Permissions API supported if ('permissions' in navigator) { @@ -265,7 +239,7 @@ angular.module('flashy.SettingsController', ['ui.router']). // Check that service workers are supported if ('serviceWorker' in navigator) { - navigator.serviceWorker.register('scripts/service-worker.js') + navigator.serviceWorker.register('service-worker.js') .then(initialiseState); } else { // Service Workers aren't supported so you should hide the push UI diff --git a/scripts/service-worker.js b/scripts/service-worker.js deleted file mode 100644 index 42afc50..0000000 --- a/scripts/service-worker.js +++ /dev/null @@ -1,46 +0,0 @@ -'use strict'; - -function showNotification(title, body, icon, data) { - var notificationOptions = { - body: body, - icon: icon ? icon : 'images/touch/chrome-touch-icon-192x192.png', - tag: 'simple-push-demo-notification', - data: data - }; - if (self.registration.showNotification) { - self.registration.showNotification(title, notificationOptions); - return; - } else { - new Notification(title, notificationOptions); - } -} - -self.addEventListener('push', function(event) { - console.log('Received a push message', event); - - // Since this is no payload data with the first version - // of Push notifications, here we'll grab some data from - // an API and use it to populate a notification - var title = 'You have cards waiting to be reviewed!'; - var message = 'check yo cards m8'; - var icon = 'flashy.ico'; - var notificationTag = 'simple-push-demo-notification'; - - // Add this to the data of the notification - var urlToOpen = '/api/subscribe/'; - - var notificationFilter = { - tag: 'simple-push-demo-notification' - }; - var notificationData = { - url: urlToOpen - }; - return showNotification(title, message, icon, notificationData); -}); - -self.addEventListener('notificationclick', function(event) { - console.log('On notification click: ', event); - - var url = event.notification.data.url; - event.waitUntil(clients.openWindow(url)); -}); diff --git a/service-worker.js b/service-worker.js new file mode 100644 index 0000000..0a8349f --- /dev/null +++ b/service-worker.js @@ -0,0 +1,46 @@ +'use strict'; + +function showNotification(title, body, icon, data) { + var notificationOptions = { + body: body, + icon: icon ? icon : '/flashy.ico', + tag: 'simple-push-demo-notification', + data: data + }; + if (self.registration.showNotification) { + self.registration.showNotification(title, notificationOptions); + return; + } else { + new Notification(title, notificationOptions); + } +} + +self.addEventListener('push', function(event) { + console.log('Received a push message', event); + + // Since this is no payload data with the first version + // of Push notifications, here we'll grab some data from + // an API and use it to populate a notification + var title = 'You have cards waiting to be reviewed!'; + var message = 'check yo cards m8'; + var icon = '/flashy.ico'; + var notificationTag = 'simple-push-demo-notification'; + + // Add this to the data of the notification + var urlToOpen = '/api/subscribe/'; + + var notificationFilter = { + tag: 'simple-push-demo-notification' + }; + var notificationData = { + url: urlToOpen + }; + return showNotification(title, message, icon, notificationData); +}); + +self.addEventListener('notificationclick', function(event) { + console.log('On notification click: ', event); + + var url = event.notification.data.url; + event.waitUntil(clients.openWindow(url)); +});