summaryrefslogtreecommitdiff
path: root/StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2017-02-16 01:24:12 +0100
committerJules Laplace <jules@okfoc.us>2017-02-16 01:24:12 +0100
commit30c49550c89c1b69c680170d2dc247eac76bd463 (patch)
tree8732652298b630b9ba15def97e59738f1c9bf7b6 /StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js
parent8f1f626384e6ba75f4fb24c27e0973260a74421b (diff)
push plugin
Diffstat (limited to 'StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js')
-rw-r--r--StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js b/StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js
new file mode 100644
index 00000000..0fc06d09
--- /dev/null
+++ b/StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js
@@ -0,0 +1,51 @@
+var messageChannel;
+
+self.addEventListener('install', function(event) {
+ self.skipWaiting();
+});
+
+self.addEventListener('push', function(event) {
+ // parse incoming message
+ var obj = {};
+ var pushData = {
+ image: 'https://avatars1.githubusercontent.com/u/60365?v=3&s=200',
+ additionalData: {}
+ };
+ if (event.data) {
+ obj = event.data.json();
+ }
+
+ console.log(obj);
+
+ // convert to push plugin API
+ for (var key in obj) {
+ if (key === 'title') {
+ pushData.title = obj[key];
+ } else if (key === 'message' || key === 'body') {
+ pushData.message = obj[key];
+ } else if (key === 'count' || key === 'msgcnt' || key === 'badge') {
+ pushData.count = obj[key];
+ } else if (key === 'sound' || key === 'soundname') {
+ pushData.sound = obj[key];
+ } else if (key === 'image') {
+ pushData.image = obj[key];
+ } else {
+ pushData.additionalData[key] = obj[key];
+ }
+ }
+
+ event.waitUntil(
+ self.registration.showNotification(pushData.title, {
+ body: pushData.message,
+ icon: pushData.image,
+ tag: 'simple-push-demo-notification-tag'
+ })
+ );
+
+ messageChannel.ports[0].postMessage(pushData);
+
+});
+
+self.addEventListener('message', function(event) {
+ messageChannel = event;
+});