-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
50 lines (44 loc) · 1.72 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Custom Subscription Button | OneSignal</title>
<link rel="stylesheet" href="style.css" />
<!--OneSignal Initialisation Code-->
<script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
<script>
window.OneSignalDeferred = window.OneSignalDeferred || [];
OneSignalDeferred.push(function (OneSignal) {
OneSignal.init({
appId: "f052cae8-01f2-44d7-96af-18491e8bcd30",
});
});
</script>
</head>
<body>
<button class="onesignal-prompt-button" role="button"></button>
<script>
// Get the button
const osButton = document.querySelector(".onesignal-prompt-button");
// Function to update button text based on subscription status
const updateButtonText = () => {
osButton.innerHTML = OneSignal.User.PushSubscription.optedIn ? "Unsubscribe from Push Notifications" : "Subscribe to Push Notifications";
};
// Set text for button on page load and subscription changes
OneSignalDeferred.push((OneSignal) => {
updateButtonText();
OneSignal.User.PushSubscription.addEventListener("change", updateButtonText);
});
// Prompt or Opt In / Opt Out, on button click
osButton.addEventListener("click", () => {
const { User, Notifications } = OneSignal;
if (User.PushSubscription.token) {
User.PushSubscription.optedIn ? User.PushSubscription.optOut() : User.PushSubscription.optIn();
} else {
Notifications.requestPermission();
}
});
</script>
</body>
</html>