-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPushNotification.js
More file actions
170 lines (155 loc) · 4.69 KB
/
PushNotification.js
File metadata and controls
170 lines (155 loc) · 4.69 KB
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
import React, { useState, useEffect } from "react"
import { notify } from "react-notify-toast"
const subscriptionUrl = "/.netlify/functions/push-subscription"
const saveSubscriptionToServer = async (subscription) => {
try {
const saveResponse = await fetch(subscriptionUrl, {
method: "POST",
body: JSON.stringify(subscription),
})
if (saveResponse.status === 201) {
return true
}
return false
} catch (error) {
return false
}
}
const removeSubscriptionFromServer = async (subscriptionEndpoint) => {
try {
const delResponse = await fetch(subscriptionUrl, {
method: "DELETE",
body: JSON.stringify({ endpoint: subscriptionEndpoint }),
})
if (delResponse.status === 204) {
return true
}
return false
} catch (error) {
return false
}
}
const hasNotificationPermission = async () => {
if (Notification.permission === "granted") {
return true
} else if (Notification.permission !== "denied") {
const permission = await Notification.requestPermission()
if (permission === "granted") {
return true
}
}
return false
}
const pushSupported = () => {
if (typeof window !== `undefined`) {
if ("PushManager" in window) {
return true
}
}
return false
}
const PushNotification = () => {
const [subscribed, setSubscribed] = useState(false)
const [working, setWorking] = useState(false)
const createSubscription = async () => {
notify.hide()
const hasPermission = await hasNotificationPermission()
if (pushSupported() && "serviceWorker" in navigator && hasPermission) {
const urlBase64ToUint8Array = (base64String) => {
const padding = "=".repeat((4 - (base64String.length % 4)) % 4)
const base64 = (base64String + padding)
.replace(/-/g, "+")
.replace(/_/g, "/")
const rawData = window.atob(base64)
const outputArray = new Uint8Array(rawData.length)
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i)
}
return outputArray
}
navigator.serviceWorker.ready
.then(async (swRegistration) => {
setWorking(true)
const pushSubscription = await swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(
"BIzWFRNmUmy6ztKkoYNJOaDudQOrbhK5zHDmeCSDX6m3L5yVd5f6Bv3xMPf6A5Cf2-X4pPULKYjL7-ddmLRKcBA"
),
})
const subSaved = await saveSubscriptionToServer(pushSubscription)
if (subSaved) {
setSubscribed(true)
localStorage.setItem("PUSH_NOTIFICATION_SUBSCRIBED", "1")
notify.show("Push subscription confirmed", "success")
setWorking(false)
} else {
notify.show("An eror occured", "error")
setWorking(false)
}
})
.catch((error) => {
console.log(error)
notify.show(
"An error occured setting up push notification",
"warning"
)
setWorking(false)
}).finally(() => setWorking(false))
} else {
notify.show("Notifications not allowed.", "error")
}
}
const unsubscribe = async () => {
notify.hide()
if (pushSupported() && subscribed) {
setWorking(true)
navigator.serviceWorker.ready
.then(async (swRegistration) => {
const subscription = await swRegistration.pushManager.getSubscription()
await subscription.unsubscribe()
removeSubscriptionFromServer(subscription.endpoint).then(() => {
localStorage.setItem("PUSH_NOTIFICATION_SUBSCRIBED", "0")
notify.show("Push notifications disabled", "success")
setWorking(false)
})
})
.catch((e) => {
setWorking(false)
})
setSubscribed(false)
}
}
useEffect(() => {
const push_sub = localStorage.getItem("PUSH_NOTIFICATION_SUBSCRIBED")
if (push_sub === "1") {
setSubscribed(true)
}
}, [])
useEffect(() => {
//always update sw
if (pushSupported() && "serviceWorker" in navigator) {
navigator.serviceWorker.ready.then((swRegistration) =>
swRegistration.update()
)
}
}, [])
if (!pushSupported()) {
return null
}
let btnText = subscribed
? "Unsubscribe from push notifications"
: "Enable push notifications"
btnText = working ? "busy..." : btnText
const callback = subscribed ? unsubscribe : createSubscription
return (
<button
className="notification-btn"
onClick={callback}
disabled={working}
tabIndex={0}
>
{btnText}
</button>
)
}
export default PushNotification