Skip to content
This repository was archived by the owner on May 2, 2021. It is now read-only.

Commit 7c2198e

Browse files
committed
migrate to onesignal
1 parent 1af76f1 commit 7c2198e

File tree

12 files changed

+63
-174
lines changed

12 files changed

+63
-174
lines changed

src/_includes/head.njk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@
103103
{%- if isHome %}
104104
<script defer src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
105105
{%- endif %}
106-
<script defer src="https://www.gstatic.com/firebasejs/8.2.3/firebase-app.js"></script>
107-
<script defer src="https://www.gstatic.com/firebasejs/8.2.3/firebase-messaging.js"></script>
106+
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
108107
<script type="module" src="/js/main.js"></script>
109108
<!-- Google Analytics -->
110109
<script>

src/_includes/header.njk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- Header Start -->
22
<header>
33
<div id="notification-bar" class="alert alert-warning" hidden>
4-
<button type="button" class="btn btn-link">Resta aggiornato sui prossimi eventi del FEVR!</button>
4+
<span class='onesignal-customlink-container'></span>
55
<button type="button" class="close" aria-label="Chiudi"><span aria-hidden="true">&times;</span></button>
66
</div>
77
<div id="token"></div>

src/_js/main.js

Lines changed: 43 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/* global firebase */
2-
import { FIREBASE_CONFIG, VAPID_KEY } from './modules/constants';
3-
1+
/* global OneSignal */
42
const toggler = document.querySelector('.navbar-toggle');
53

64
if (toggler) {
@@ -14,83 +12,50 @@ if (toggler) {
1412
});
1513
}
1614

17-
async function subscribe(sw) {
18-
const result = await Notification.requestPermission();
19-
20-
if (result !== 'granted' || !window.firebase) {
21-
return;
22-
}
23-
// Initialize Firebase
24-
25-
firebase.initializeApp(FIREBASE_CONFIG);
26-
27-
const messaging = firebase.messaging();
28-
29-
try {
30-
const token = await messaging.getToken({
31-
serviceWorkerRegistration: sw,
32-
vapidKey: VAPID_KEY,
33-
});
34-
if (!token) {
35-
return;
36-
}
37-
38-
console.log(token);
39-
document.getElementById('token').textContent = token;
40-
} catch (err) {
41-
console.error('An error occurred while retrieving token. ', err);
42-
}
43-
44-
messaging.onMessage((payload) => {
45-
console.log('Message received. ', payload);
15+
window.OneSignal = window.OneSignal || [];
16+
OneSignal.push(function () {
17+
OneSignal.init({
18+
appId:
19+
process.env.ELEVENTY_ENV !== 'production'
20+
? '7fd49bb1-4042-42fe-acbc-d75e77584ada'
21+
: '3a8c18f3-02ec-469c-ae86-11d0eed8f39c',
4622
});
47-
}
48-
49-
// async function notify() {
50-
// const result = await Notification.requestPermission();
51-
// if (result !== 'granted') {
52-
// return;
23+
});
24+
25+
OneSignal.push([
26+
'getNotificationPermission',
27+
(permission) => {
28+
const isPushSupported = OneSignal.isPushNotificationsSupported();
29+
if (isPushSupported && permission === 'default') {
30+
document.getElementById('notification-bar').hidden = false;
31+
}
32+
OneSignal.on('notificationPermissionChange', () => {
33+
document.getElementById('notification-bar').hidden = true;
34+
});
35+
},
36+
]);
37+
38+
// function registerNotifier() {
39+
// const hideNotificationBar =
40+
// localStorage.getItem('notificationBar') === 'hide';
41+
// if (!hideNotificationBar) {
42+
// const bar = document.getElementById('notification-bar');
43+
44+
// const closeBar = () => {
45+
// bar.hidden = true;
46+
// localStorage.setItem('notificationBar', 'hide');
47+
// };
48+
49+
// bar.hidden = false;
50+
// bar.querySelector('button.close').addEventListener('click', closeBar);
51+
52+
// bar.querySelector('.btn.btn-link').addEventListener('click', async () => {
53+
// await Notification.requestPermission();
54+
// closeBar();
55+
// });
5356
// }
54-
// const body = 'hello';
55-
// const icon = '/apple-touch-icon.png';
56-
// new Notification('Hello!', {
57-
// body,
58-
// icon,
59-
// });
6057
// }
6158

62-
function registerNotifier(sw) {
63-
const hideNotificationBar =
64-
localStorage.getItem('notificationBar') === 'hide';
65-
if (!hideNotificationBar) {
66-
const bar = document.getElementById('notification-bar');
67-
68-
const closeBar = () => {
69-
bar.hidden = true;
70-
localStorage.setItem('notificationBar', 'hide');
71-
};
72-
73-
bar.hidden = false;
74-
bar.querySelector('button.close').addEventListener('click', closeBar);
75-
76-
bar.querySelector('.btn.btn-link').addEventListener('click', async () => {
77-
const result = await Notification.requestPermission();
78-
closeBar();
79-
if (result === 'granted') {
80-
subscribe(sw);
81-
}
82-
});
83-
} else {
84-
subscribe(sw);
85-
}
86-
}
87-
88-
if (process.env.NODE_ENV === 'production') {
89-
if ('serviceWorker' in navigator) {
90-
navigator.serviceWorker
91-
.register('/service-worker.js')
92-
.then(registerNotifier);
93-
}
94-
} else {
95-
registerNotifier();
59+
if ('serviceWorker' in navigator) {
60+
navigator.serviceWorker.register('/OneSignalSDKWorker.js');
9661
}

src/_js/modules/constants.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/_js/modules/message-worker.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/_js/service-worker-dev.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Empty service worker for dev
2+
console.log('dev service worker');

src/_templates/script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ module.exports = class Scripts {
2424
'process.env.NODE_ENV': JSON.stringify(
2525
process.env.ELEVENTY_ENV || 'development',
2626
),
27+
'process.env.ELEVENTY_ENV': JSON.stringify(
28+
process.env.ELEVENTY_ENV || 'development',
29+
),
2730
},
2831
});
2932
return outputFiles[0].contents;

src/firebase-messaging-sw.11ty.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/service-worker.11ty.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module.exports = class ServiceWorker extends Scripts {
55
constructor() {
66
super();
77
this.permalink = '/service-worker.js';
8-
this.src = 'service-worker.js';
8+
this.src =
9+
process.env.ELEVENTY_ENV !== 'production'
10+
? 'service-worker-dev.js'
11+
: 'service-worker.js';
912
}
1013
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-env serviceworker */
2+
importScripts('service-worker.js');
3+
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

0 commit comments

Comments
 (0)