1+ var reviewRedirect , title , options ;
2+ title = 'We Need Your Help!' ;
3+ options = {
4+ body : '' ,
5+ icon : '/images/logo-192x192.png' ,
6+ sound : '/sounds/alert.wav' ,
7+ actions : [
8+ {
9+ action : 'review' ,
10+ title : 'Yes!'
11+ }
12+ ] ,
13+ tag : 'help-notification' ,
14+ vibrate : [ 500 , 110 , 500 , 110 , 450 ]
15+ } ;
16+ function helpNotification ( name , id ) {
17+ if ( name == '404' )
18+ options . body = '' ;
19+ else {
20+ reviewRedirect = 'location/rating/' + id + '/5' ;
21+ options . body = 'Would you like to add a review to ' + name + '?' ;
22+ }
23+ //self.registration.showNotification(title, options);
24+ } ;
25+
26+ function sendNotification ( ) {
27+ //check to send here
28+ sendMessage ( { type : 'refresh' } ) ;
29+ if ( options . body !== '' )
30+ self . registration . showNotification ( title , options ) ;
31+ }
32+
33+ function sendMessageToClient ( client , msg ) {
34+ return new Promise ( function ( resolve , reject ) {
35+ var msg_chan = new MessageChannel ( ) ;
36+
37+ msg_chan . port1 . onmessage = function ( event ) {
38+ if ( event . data . error ) {
39+ reject ( event . data . error ) ;
40+ } else {
41+ resolve ( event . data ) ;
42+ }
43+ } ;
44+
45+ client . postMessage ( msg , [ msg_chan . port2 ] ) ;
46+ } ) ;
47+ }
48+
49+ function sendMessage ( content ) {
50+ clients . matchAll ( ) . then ( clients => {
51+ clients . forEach ( client => {
52+ sendMessageToClient ( client , content ) ;
53+ } )
54+ } )
55+ }
56+
57+ self . addEventListener ( 'install' , function ( e ) {
58+ e . waitUntil ( self . skipWaiting ( ) ) ;
59+ console . log ( 'Service Worker Installed' )
60+ } )
61+
62+ self . addEventListener ( 'activate' , function ( e ) {
63+ e . waitUntil ( self . clients . claim ( ) )
64+ console . log ( 'Service Worker Activated' ) ;
65+ } )
66+
67+ self . addEventListener ( 'fetch' , function ( e ) {
68+ //console.log('fetching '+e);
69+ } )
70+
71+ self . addEventListener ( 'notificationclick' , function ( e ) {
72+ switch ( e . action ) {
73+ case 'review' :
74+ sendMessage ( {
75+ type : 'redirect' ,
76+ url : reviewRedirect
77+ } ) ;
78+ break ;
79+ }
80+ e . notification . close ( ) ;
81+ } )
82+
83+ self . addEventListener ( 'message' , function ( e ) {
84+ helpNotification ( e . data . name , e . data . id ) ;
85+ } )
86+
87+ //send first notification
88+ setTimeout ( sendNotification , 5000 ) ;
89+ //Starting the push notification clock
90+ setTimeout ( setInterval ( sendNotification , 3600000 ) , 5000 ) ;
0 commit comments