-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNotification.js
267 lines (263 loc) · 7.55 KB
/
Notification.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import React from 'react'
import fetch from 'isomorphic-unfetch'
import SnackBar from './Snackbar'
import NProgress from 'nprogress'
import db from '../lib/db'
export default class Notification extends React.Component {
constructor (props) {
super(props)
this.state = {
checked: false,
show: false,
message: ''
}
}
componentDidMount () {
if (Notification.permission === 'denied') {
console.log('User has blocked push notification.')
return
}
// Get `push notification` subscription
// If `serviceWorker` is registered and ready
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
registration.pushManager
.getSubscription()
.then(subscription => {
// If already access granted, enable push button status
if (subscription) {
this.setState({
checked: true
})
} else {
this.setState({
checked: false
})
}
})
.catch(error => {
console.error('Error occurred while enabling push ', error)
this.setState({
show: true,
message: 'Error occurred while enabling push '
})
})
})
}
}
subscribePush () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
if (!registration.pushManager) {
this.setState({
show: true,
message: "Sorry, Push notification isn't supported in your browser."
})
return
}
NProgress.start()
// To subscribe `push notification` from push manager
registration.pushManager
.subscribe({
userVisibleOnly: true // Always show notification when received
})
.then(subscription => {
console.info('Push notification subscribed.')
console.log(subscription)
return this.saveSubscriptionID(subscription)
})
.then(() => {
NProgress.done()
this.setState({
checked: true,
show: true,
message: 'Successfully subscribed to push notifications.'
})
})
.catch(error => {
NProgress.done()
this.setState({
checked: false,
show: true,
message: 'Push notification subscription failed'
})
console.error('Push notification subscription error: ', error)
})
})
} else {
this.setState({
show: true,
message: 'Some scripts are not loaded yet, try again after sometime'
})
}
}
unsubscribePush () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then(registration => {
// Get `push subscription`
registration.pushManager
.getSubscription()
.then(subscription => {
// If no `push subscription`, then return
if (!subscription) {
this.setState({
show: true,
message: 'Unable to unregister push notification.'
})
return
}
NProgress.start()
// Unsubscribe `push notification`
subscription
.unsubscribe()
.then(() => {
console.info('Push notification unsubscribed.')
console.log(subscription)
return this.deleteSubscriptionID(subscription)
})
.then(() => {
NProgress.done()
this.setState({
checked: false,
show: true,
message: 'Successfully unsubscribed from push notifications.'
})
})
.catch(function (error) {
NProgress.done()
console.error(error)
this.setState({
show: true,
message: 'Failed to unsubscribe push notification.'
})
})
})
.catch(error => {
console.error(error)
console.error('Failed to unsubscribe push notification.')
this.setState({
show: true,
message: 'Failed to unsubscribe push notification.'
})
})
})
} else {
this.setState({
show: true,
message: 'Some scripts are not loaded yet, try again after sometime'
})
}
}
saveSubscriptionID (subscription) {
var subscriptionId =
subscription.endpoint.split('gcm/send/')[1] ||
subscription.endpoint.split(/\/wpush\/v\d\//)[1]
console.log('Subscription ID', subscriptionId)
return fetch(`${db.baseUrl}/subscriptions`, {
method: 'POST',
body: JSON.stringify({
subscriptionId: subscriptionId,
subscription: subscription
}),
headers: {
'Content-Type': 'application/json'
}
})
}
deleteSubscriptionID (subscription) {
var subscriptionId = subscription.endpoint.split('gcm/send/')[1]
return fetch(`${db.baseUrl}/subscriptions/` + subscriptionId, {
method: 'DELETE'
})
}
handelInput (e) {
console.log('clicked')
if (this.state.checked) {
console.log('unsubscribing')
this.unsubscribePush()
} else {
console.log('subscribing')
this.subscribePush()
}
}
onSanckbarClose () {
console.log('snackbar closed')
this.setState({
show: false,
message: ''
})
}
render () {
return (
<li>
<label className='switch'>
<input
onClick={this.handelInput.bind(this)}
type='checkbox'
checked={this.state.checked}
/>
<div className='slider round' />
</label>
<SnackBar
onClose={this.onSanckbarClose.bind(this)}
show={this.state.show}
timer={4000}
>
<p>{this.state.message}</p>
</SnackBar>
<style jsx>
{`
.switch {
position: relative;
display: inline-block;
width: 50px;
height: 25px;
}
.switch input {
display: none;
}
.switch .slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #ccc;
-webkit-transition: all 0.4s;
transition: all 0.4s;
cursor: pointer;
}
.switch .slider.round {
border-radius: 25px;
}
.switch .slider.round:before {
border-radius: 25px;
}
.switch .slider:before {
position: absolute;
content: '🔕';
font-size: 14px;
line-height: 22px;
width: 22px;
height: 22px;
left: 2px;
bottom: 2px;
background-color: white;
-webkit-transition: all 0.4s;
transition: all 0.4s;
}
input:checked + .slider {
background: #ff6600;
}
input:checked + .slider:before {
content: '🔔';
text-align: center;
-webkit-transform: translateX(24px);
transform: translateX(24px);
}
`}
</style>
</li>
)
}
}