Skip to content

Commit 02b962c

Browse files
committed
Fixed all issues except changing check to controller
1 parent cd1fedb commit 02b962c

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

app/public/js/service_worker_script.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
var url= window.location.href;
22
var pos = url.search('using_pwa');
33

4-
if (pos+1 && url[pos+8] && 'serviceWorker' in navigator) {
5-
navigator.serviceWorker
6-
.register('service-worker.js')
7-
.then(function(registration) {
8-
if (!navigator.serviceWorker.controller){
9-
location.reload();
10-
}
11-
sendNewLocation();
12-
})
13-
.catch(function(err) {
14-
console.log("Service Worker Failed to Register", err);
15-
});
4+
function initServiceWorker(){
5+
if (pos+1 && url[pos+8] && 'serviceWorker' in navigator) {
6+
navigator.serviceWorker
7+
.register('service-worker.js')
8+
.then(function(registration) {
9+
if (!navigator.serviceWorker.controller){
10+
location.reload();
11+
}
12+
sendNewLocation();
13+
})
14+
.catch(function(err) {
15+
console.log("Service Worker Failed to Register", err);
16+
});
17+
}
1618
}
1719

18-
if ('serviceWorker' in navigator){
19-
navigator.serviceWorker.addEventListener('message', function(e) {
20-
if(e.data.type=='redirect') {
21-
location.replace(e.data.url);
22-
} else if(e.data.type='refresh') {
23-
sendNewLocation();
24-
}
25-
});
20+
function initListener(){
21+
if ('serviceWorker' in navigator){
22+
navigator.serviceWorker.addEventListener('message', function(e) {
23+
if(e.data.type=='redirect') {
24+
location.replace(e.data.url);
25+
} else if(e.data.type='refresh') {
26+
sendNewLocation();
27+
}
28+
});
29+
}
2630
}
2731

32+
33+
initServiceWorker();
34+
initListener();
35+
2836
function sendNewLocation() {
2937
getCurrentGeolocation().then( function(latlng){
3038
lng=latlng.lng();

app/public/service-worker.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title = 'We Need Your Help!';
33
options = {
44
body: '',
55
icon: '/images/logo-192x192.png',
6-
sound: '/sounds/alert.wav',
6+
sound: '/sounds/alert.mp3',
77
actions: [
88
{
99
action: 'review',
@@ -13,18 +13,16 @@ options = {
1313
tag: 'help-notification',
1414
vibrate: [500,110,500,110,450]
1515
};
16-
function helpNotification(name,id) {
16+
function updateHelpNotificationData(name,id) {
1717
if(name=='404')
1818
options.body='';
1919
else{
2020
reviewRedirect='location/rating/'+id+'/5';
2121
options.body='Would you like to add a review to '+name+'?';
2222
}
23-
//self.registration.showNotification(title, options);
2423
};
2524

26-
function sendNotification() {
27-
//check to send here
25+
function sendHelpNotification() {
2826
sendMessage({type: 'refresh'});
2927
if(options.body!=='')
3028
self.registration.showNotification(title,options);
@@ -64,9 +62,6 @@ self.addEventListener('activate', function(e) {
6462
console.log('Service Worker Activated');
6563
})
6664

67-
self.addEventListener('fetch',function(e) {
68-
//console.log('fetching '+e);
69-
})
7065

7166
self.addEventListener('notificationclick', function(e) {
7267
switch(e.action) {
@@ -81,10 +76,10 @@ self.addEventListener('notificationclick', function(e) {
8176
})
8277

8378
self.addEventListener('message', function(e) {
84-
helpNotification(e.data.name,e.data.id);
79+
updateHelpNotificationData(e.data.name,e.data.id);
8580
})
8681

8782
//send first notification
88-
setTimeout(sendNotification,5000);
83+
setTimeout(sendHelpNotification,5000);
8984
//Starting the push notification clock
90-
setTimeout(setInterval(sendNotification,3600000),5000);
85+
setTimeout(setInterval(sendHelpNotification,3600000),5000);

app/public/sounds/alert.mp3

46.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)