-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifications.js
162 lines (144 loc) · 5.38 KB
/
notifications.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* global ServiceWorkerRegistration */
window.Notifications = function(ui, s) {
var isPushEnabled = false;
function sendSubscriptionToServer(sub) {
return s.sendSubscriptionToServer(sub);
}
function cancelSubscriptionFromServer(sub) {
return s.cancelSubscriptionFromServer(sub);
}
this.init = function() {
// Are Notifications supported in the service worker?
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
ui.notSupported('Notifications not supported');
return;
}
// Check the current Notification permission
// If it's denied, it's a permanent block until the user changes the permission
if (Notification.permission === 'denied') {
ui.notSupported('The user has blocked notifications');
return;
}
// Is push messaging supported?
if (!('PushManager' in window)) {
ui.notSupported('Push messaging not supported');
return;
}
ui.load();
// We need the service worker registration to check for a subscription
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
// Do we already have a push message subscription?
serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) {
// Enable any UI which subscribes / unsubscribes from push messages
ui.enable();
if (!subscription) {
// We aren't subscribed to push, so set UI to allow the user to enable push
return;
}
// Keep your server in sync with the latest subscriptionId
// var sub = JSON.parse(JSON.stringify(subscription));
// return sendSubscriptionToServer(sub);
// Set your UI to show they have subscribed for push messages
var sub = JSON.parse(JSON.stringify(subscription));
ui.subscribed(sub);
isPushEnabled = true;
})
.catch(function(err) {
console.error('Error during getSubscription()', err);
});
});
};
function subscribe() {
// Disable the button so it can't be changed while we process the permission request
ui.enable();
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
var _subscription;
var _sub;
// Subscribe the browser
serviceWorkerRegistration.pushManager.subscribe({ userVisibleOnly: true })
// Send the subscription.endpoint to the server to save it in the db
.then(function(subscription) {
_subscription = subscription;
_sub = JSON.parse(JSON.stringify(subscription));
return sendSubscriptionToServer(_sub);
})
// The subscription was successful, show it
.then(function() {
ui.subscribed(_sub);
isPushEnabled = true;
})
.catch(function(err) {
if (Notification.permission === 'denied') {
// The user denied the notification permission which
// means we failed to subscribe and the user will need
// to manually change the notification permission to
// subscribe to push messages
ui.status('Permission for Notifications was denied by the user');
ui.disable();
}
else {
// A problem occurred with the subscription; common reasons
// include network errors, and lacking gcm_sender_id and/or
// gcm_user_visible_only in the manifest.
ui.status('Unable to subscribe to push', err);
_subscription.unsubscribe();
ui.unsubscribed();
isPushEnabled = false;
ui.enable();
}
});
});
}
function unsubscribe() {
ui.disable();
navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
// To unsubscribe from push messaging, you need get the subscription object, which you can call unsubscribe() on
serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) {
// Check we have a subscription to unsubscribe
if (!subscription) {
// No subscription object, so set the state to allow the user to subscribe to push
ui.unsubscribed();
isPushEnabled = false;
return;
}
var sub = JSON.parse(JSON.stringify(subscription));
// We have a subscription, so call unsubscribe on it
subscription.unsubscribe()
.then(function(successful) {
if (successful) {
ui.unsubscribed();
isPushEnabled = false;
ui.enable();
// Make a request to your server to remove
// the subscriptionId from your data store so you
// don't attempt to send them push messages anymore.
// The client doesn't care about the success of this
cancelSubscriptionFromServer(sub);
}
})
.catch(function(err) {
// We failed to unsubscribe, this can lead to
// an unusual state, so may be best to remove
// the users data from your data store and
// inform the user that you have done so
console.error('Unsubscription error: ', err);
ui.unsubscribed();
ui.enable();
});
})
.catch(function(err) {
console.error('Error thrown while unsubscribing from push messaging.', err);
});
});
}
this.toggle = function() {
if (isPushEnabled) {
unsubscribe();
}
else {
subscribe();
}
};
};