1- /* global firebase */
2- import { FIREBASE_CONFIG , VAPID_KEY } from './modules/constants' ;
3-
1+ /* global OneSignal */
42const toggler = document . querySelector ( '.navbar-toggle' ) ;
53
64if ( 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}
0 commit comments