summaryrefslogtreecommitdiff
path: root/StoneIsland/plugins/phonegap-plugin-push/src/browser/ServiceWorker.js
blob: 0fc06d099fcebb9e44b31fc3da0b2a58d1f018eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
});