-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send push notifications to users nearby unrated locations #688
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
var url= window.location.href; | ||
var pos = url.search('using_pwa'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this check to the server. HomeController.php has a method that would be called for route "/". Store the true/false value of whether the request was using_pwa into the server's session so it is available as the user navigates around the site. This value on the server can be used to prevent including these service worker scripts when not in a PWA. |
||
|
||
function initServiceWorker(){ | ||
if (pos+1 && url[pos+8] && 'serviceWorker' in navigator) { | ||
navigator.serviceWorker | ||
.register('service-worker.js') | ||
.then(function(registration) { | ||
if (!navigator.serviceWorker.controller){ | ||
location.reload(); | ||
} | ||
sendNewLocation(); | ||
}) | ||
.catch(function(err) { | ||
console.log("Service Worker Failed to Register", err); | ||
}); | ||
} | ||
} | ||
|
||
function initListener(){ | ||
if ('serviceWorker' in navigator){ | ||
navigator.serviceWorker.addEventListener('message', function(e) { | ||
if(e.data.type=='redirect') { | ||
location.replace(e.data.url); | ||
} else if(e.data.type='refresh') { | ||
sendNewLocation(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
initServiceWorker(); | ||
initListener(); | ||
|
||
function sendNewLocation() { | ||
getCurrentGeolocation().then( function(latlng){ | ||
lng=latlng.lng(); | ||
lat=latlng.lat(); | ||
var urlGet = '/api/location/nearby/'+lng+'/'+lat; | ||
var ajaxReq = $.ajax({ | ||
type: 'get', | ||
url: urlGet, | ||
dataType: 'json', | ||
success: function(data, status, xhr){ | ||
navigator.serviceWorker.controller.postMessage(data); | ||
}, | ||
error: function(xhr, reason, ex){ | ||
var sampleData={ | ||
name: '404', | ||
id:'0' | ||
}; | ||
navigator.serviceWorker.controller.postMessage(sampleData); | ||
console.log("No nearby locations found at "+urlGet); | ||
} | ||
}); | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
var reviewRedirect, title, options; | ||
title = 'We Need Your Help!'; | ||
options = { | ||
body: '', | ||
icon: '/images/logo-192x192.png', | ||
sound: '/sounds/alert.mp3', | ||
actions: [ | ||
{ | ||
action: 'review', | ||
title: 'Yes!' | ||
} | ||
], | ||
tag: 'help-notification', | ||
vibrate: [500,110,500,110,450] | ||
}; | ||
function updateHelpNotificationData(name,id) { | ||
if(name=='404') | ||
options.body=''; | ||
else{ | ||
reviewRedirect='location/rating/'+id+'/5'; | ||
options.body='Would you like to add a review to '+name+'?'; | ||
} | ||
}; | ||
|
||
function sendHelpNotification() { | ||
sendMessage({type: 'refresh'}); | ||
if(options.body!=='') | ||
self.registration.showNotification(title,options); | ||
} | ||
|
||
function sendMessageToClient(client, msg){ | ||
return new Promise(function(resolve, reject){ | ||
var msg_chan = new MessageChannel(); | ||
|
||
msg_chan.port1.onmessage = function(event){ | ||
if(event.data.error){ | ||
reject(event.data.error); | ||
}else{ | ||
resolve(event.data); | ||
} | ||
}; | ||
|
||
client.postMessage(msg, [msg_chan.port2]); | ||
}); | ||
} | ||
|
||
function sendMessage(content){ | ||
clients.matchAll().then(clients => { | ||
clients.forEach(client => { | ||
sendMessageToClient(client, content); | ||
}) | ||
}) | ||
} | ||
|
||
self.addEventListener('install', function(e) { | ||
e.waitUntil(self.skipWaiting()); | ||
console.log('Service Worker Installed') | ||
}) | ||
|
||
self.addEventListener('activate', function(e) { | ||
e.waitUntil(self.clients.claim()) | ||
console.log('Service Worker Activated'); | ||
}) | ||
|
||
|
||
self.addEventListener('notificationclick', function(e) { | ||
switch(e.action) { | ||
case 'review': | ||
sendMessage({ | ||
type: 'redirect', | ||
url: reviewRedirect | ||
}); | ||
break; | ||
} | ||
e.notification.close(); | ||
}) | ||
|
||
self.addEventListener('message', function(e) { | ||
updateHelpNotificationData(e.data.name,e.data.id); | ||
}) | ||
|
||
//send first notification | ||
setTimeout(sendHelpNotification,5000); | ||
//Starting the push notification clock | ||
setTimeout(setInterval(sendHelpNotification,3600000),5000); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
<div id="copyright"></div> | ||
<script src="/js/jquery-3.1.1.js" type="text/javascript"></script> | ||
<script src="/js/utils.js" type="text/javascript"></script> | ||
<script type="text/javascript" async defer | ||
src="//maps.googleapis.com/maps/api/js?key={{ $google_map_api_key }}&callback=initMap"> | ||
</script> | ||
<script src="/js/service_worker_script.js" type="text/javascript"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When was this needed? What error did you run into? Was default_location.latitude undefined at some point?