Browse Source

Improved PUSH-notifications

master
Igor Zhukov 8 years ago
parent
commit
24a5402fbb
  1. 70
      app/js/lib/push_worker.js
  2. 2
      app/js/offline_manager.js
  3. 4
      app/service_worker.js

70
app/js/lib/push_worker.js

@ -1,5 +1,3 @@
'use strict';
console.log('[SW] Push worker started') console.log('[SW] Push worker started')
@ -27,22 +25,23 @@ self.addEventListener('push', function(event) {
var obj = event.data.json() var obj = event.data.json()
console.log('[SW] push', obj) console.log('[SW] push', obj)
if (!obj.badge) { if (!obj.badge) {
event.waitUntil(self.registration.showNotification('Telegram').then(function () { var promise = self.registration.showNotification('Telegram').then(function () {
return closeAllNotifications(obj) return closeAllNotifications(obj)
})) }).catch(function (error) {
console.error('Show notification error', error)
})
if ('waitUntil' in event) {
event.waitUntil(promise)
}
} else { } else {
fireNotification(obj, event) fireNotification(obj, event)
} }
}) })
self.addEventListener('activate', function(event) {
event.waitUntil(clients.claim())
})
self.addEventListener('message', function(event) { self.addEventListener('message', function(event) {
console.log('[SW] on message', event.data) console.log('[SW] on message', event.data)
var client = event.ports[0] || event.source var client = event.ports && event.ports[0] || event.source
if (event.data.type == 'ping') { if (event.data.type == 'ping') {
if (event.data.localNotifications) { if (event.data.localNotifications) {
lastAliveTime = +(new Date()) lastAliveTime = +(new Date())
@ -76,7 +75,7 @@ function fireNotification(obj, event) {
var nowTime = +(new Date()) var nowTime = +(new Date())
if (nowTime - lastAliveTime < 60000) { if (nowTime - lastAliveTime < 60000) {
console.log('Supress notification because some instance is alive') console.log('Supress notification because some instance is alive')
return false // return false
} }
if (muteUntil && nowTime < muteUntil) { if (muteUntil && nowTime < muteUntil) {
console.log('Supress notification because mute for ', (muteUntil - nowTime) / 60000, 'min') console.log('Supress notification because mute for ', (muteUntil - nowTime) / 60000, 'min')
@ -127,9 +126,13 @@ function fireNotification(obj, event) {
if (event && event.notification) { if (event && event.notification) {
pushToNotifications(event.notification) pushToNotifications(event.notification)
} }
}).catch(function (error) {
console.error('Show notification promise', error)
}) })
event.waitUntil(finalPromise) if ('waitUntil' in event) {
event.waitUntil(finalPromise)
}
return true return true
} }
@ -163,17 +166,24 @@ function closeAllNotifications(obj) {
} catch (e) {} } catch (e) {}
} }
var p = self.registration.getNotifications({}).then(function(notifications) { var promise
for (var i = 0, len = notifications.length; i < len; i++) { if ('getNotifications' in self.registration) {
try { promise = self.registration.getNotifications({}).then(function(notifications) {
notifications[i].close() for (var i = 0, len = notifications.length; i < len; i++) {
} catch (e) {} try {
} notifications[i].close()
}) } catch (e) {}
}
}).catch(function (error) {
console.error('Offline register SW error', error)
})
} else {
promise = Promise.resolve()
}
notifications = [] notifications = []
return p return promise
} }
@ -193,7 +203,7 @@ self.addEventListener('notificationclick', function(event) {
return return
} }
event.waitUntil(clients.matchAll({ var promise = clients.matchAll({
type: 'window' type: 'window'
}).then(function(clientList) { }).then(function(clientList) {
notification.data.action = action notification.data.action = action
@ -210,7 +220,13 @@ self.addEventListener('notificationclick', function(event) {
if (clients.openWindow) { if (clients.openWindow) {
return clients.openWindow(baseUrl) return clients.openWindow(baseUrl)
} }
})) }).catch(function (error) {
console.error('Clients.matchAll error', error)
})
if ('waitUntil' in event) {
event.waitUntil(promise)
}
}) })
self.addEventListener('notificationclose', onCloseNotification) self.addEventListener('notificationclose', onCloseNotification)
@ -242,12 +258,12 @@ self.addEventListener('notificationclose', onCloseNotification)
db.createObjectStore(dbStoreName) db.createObjectStore(dbStoreName)
} }
if (!request) { if (!request) {
throw new Exception() return reject()
} }
} catch (error) { } catch (error) {
console.error('error opening db', error.message) console.error('error opening db', error.message)
idbIsAvailable = false idbIsAvailable = false
return $q.reject(error) return reject(error)
} }
var finished = false var finished = false
@ -320,7 +336,7 @@ self.addEventListener('notificationclose', onCloseNotification)
request.onsuccess = function (event) { request.onsuccess = function (event) {
var result = event.target.result var result = event.target.result
if (result === undefined) { if (result === undefined) {
reject() resolve()
} else { } else {
resolve(result) resolve(result)
} }
@ -348,12 +364,18 @@ self.addEventListener('notificationclose', onCloseNotification)
IDBManager.getItem('push_mute_until').then(function (newMuteUntil) { IDBManager.getItem('push_mute_until').then(function (newMuteUntil) {
muteUntil = Math.max(muteUntil || 0, newMuteUntil || 0) || false muteUntil = Math.max(muteUntil || 0, newMuteUntil || 0) || false
}).catch(function (error) {
console.error('IDB error', error)
}) })
IDBManager.getItem('push_lang').then(function (newLang) { IDBManager.getItem('push_lang').then(function (newLang) {
lang = newLang || {} lang = newLang || {}
}).catch(function (error) {
console.error('IDB error', error)
}) })
IDBManager.getItem('push_settings').then(function (newSettings) { IDBManager.getItem('push_settings').then(function (newSettings) {
settings = newSettings || {} settings = newSettings || {}
}).catch(function (error) {
console.error('IDB error', error)
}) })

2
app/js/offline_manager.js

@ -55,6 +55,8 @@
} }
}) })
}) })
}).catch(function (error) {
console.error('Offline register SW error', error)
}) })
} else { } else {
// Otherwise, use AppCache. // Otherwise, use AppCache.

4
app/service_worker.js

@ -1,3 +1,3 @@
// Version 45 importScripts('js/lib/push_worker.js')
importScripts('js/lib/push_worker.js?45')
// Version 49
Loading…
Cancel
Save