Push Messages #1

This commit is contained in:
JonatanRek 2019-10-08 11:12:47 +02:00
parent 92f8486f91
commit 1f456aaaac
3 changed files with 106 additions and 75 deletions

View File

@ -1,10 +1,37 @@
var pending = false; var pending = false;
var firebaseConfig = {
apiKey: "AIzaSyBFZjXvnCMpGurSWEuVgHkE9jD9jxGJhx8",
authDomain: "test-push-notf.firebaseapp.com",
databaseURL: "https://test-push-notf.firebaseio.com",
projectId: "test-push-notf",
storageBucket: "",
messagingSenderId: "93473765978",
appId: "1:93473765978:web:5d959a487fe5382480f663"
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
window.addEventListener('load', () => { window.addEventListener('load', () => {
navigator.serviceWorker.register('serviceWorker.js') navigator.serviceWorker.register('serviceWorker.js')
.then(registration => { .then(registration => {
console.log('Service Worker is registered', registration); console.log('Service Worker is registered', registration);
messaging.useServiceWorker(registration);
messaging.usePublicVapidKey('BDYQ7X7J7PX0aOFNqB-CivQeqLq4-SqCxQJlDfJ6yNnQeYRoK8H2KOqxHRh47fLrbUhC8O3tve67MqJAIqox7Ng');
messaging.requestPermission().then(function () {
console.log("Notification permission granted.");
return messaging.getToken()
})
.then(function(token) {
console.log("token is : " + token);
})
.catch(function (err) {
console.log("Unable to get permission to notify.", err);
});
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
});
}) })
.catch(err => { .catch(err => {
console.error('Registration failed:', err); console.error('Registration failed:', err);
@ -191,7 +218,7 @@ $( '[name="room"]' ).change(function (e) {
}); });
} }
}); });
/*
var windowLoc = $(location).attr('pathname'); var windowLoc = $(location).attr('pathname');
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/")); windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
console.log(); console.log();
@ -227,7 +254,7 @@ if (windowLoc == "/") {
}); });
} }
},4000); },4000);
} }*/
//Graphs //Graphs
@ -235,6 +262,7 @@ $('.graph-period').on('click', function (e) {
var subId = $(this).attr('data-sub-device-id'); var subId = $(this).attr('data-sub-device-id');
var period = $(this).attr('data-period'); var period = $(this).attr('data-period');
var groupBy = $(this).attr('data-group'); var groupBy = $(this).attr('data-group');
ajaxChart(subId, period, groupBy); ajaxChart(subId, period, groupBy);
}); });

View File

@ -1,3 +1,5 @@
<script src="./app/templates/js/jquery.js"></script> <script src="./app/templates/js/jquery.js"></script>
<script src="./app/templates/js/post.js"></script> <script src="https://www.gstatic.com/firebasejs/7.1.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.1.0/firebase-messaging.js"></script>
<script src="./app/templates/js/script.js"></script> <script src="./app/templates/js/script.js"></script>
<script src="./app/templates/js/post.js"></script>

View File

@ -1,3 +1,6 @@
importScripts('https://www.gstatic.com/firebasejs/7.1.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.1.0/firebase-messaging.js');
/** /**
* Cache version, change name to force reload * Cache version, change name to force reload
*/ */
@ -22,37 +25,31 @@ var CACHE_FILES = [
'assets/logo.svg' 'assets/logo.svg'
]; ];
/**
* Service worker 'install' event.
* If all the files are successfully cached, then the service worker will be installed.
* If any of the files fail to download, then the install step will fail.
*/
this.addEventListener('install', function(event) { this.addEventListener('install', function(event) {
console.log('Install');
}); });
/**
* After a service worker is installed and the user navigates to a different page or refreshes,
* the service worker will begin to receive fetch events.
*
* Network-first approach: if online, request is fetched from network and not from cache
*/
self.addEventListener('push', function(event) { self.addEventListener('push', function(event) {
console.log('Received a push message', event); console.log('Received a push message', event);
if (!firebase.apps.length) {
firebase.initializeApp({
'messagingSenderId': '93473765978'
});
}
var title = 'Notification'; const messaging = firebase.messaging();
var body = 'There is newly updated content available on the site. Click to see more.'; messaging.setBackgroundMessageHandler(function(payload) {
var icon = 'https://raw.githubusercontent.com/deanhume/typography/gh-pages/icons/typography.png'; console.log('[firebase-messaging-sw.js] Received background message ', payload);
var tag = 'simple-push-demo-notification-tag'; // Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/itwonders-web-logo.png'
};
event.waitUntil( return self.registration.showNotification(notificationTitle,
self.registration.showNotification(title, { notificationOptions);
body: body, });
icon: icon,
tag: tag
})
);
}); });
self.addEventListener('sync', function(event) { self.addEventListener('sync', function(event) {
@ -75,4 +72,8 @@ self.addEventListener('notificationclick', function(e) {
}); });
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.