-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
172 lines (144 loc) · 5.15 KB
/
index.html
File metadata and controls
172 lines (144 loc) · 5.15 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
171
172
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<link href='https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Playfair+Display:400italic,700italic' rel='stylesheet' type='text/css'>
<script src="main.js"></script>
<link rel="manifest" href="/manifest.json">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="container__item landing-page-container">
<div class="content__wrapper">
<header class="header">
<!-- <div class="menu-icon header__item">
<span class="menu-icon__line"></span>
</div> -->
<h1 class="heading header__item">Web Push Notifications</h1>
<!-- <ul class="social-container header__item">
<li class="social__icon social__icon--fb">
<img src="https://s29.postimg.org/3ldyta4qb/image.png" alt="facebook">
</li>
<li class="social__icon social__icon--dr">
<img src="https://s29.postimg.org/vqltn8fhv/image.png" alt="dribbble">
</li>
<li class="social__icon social__icon--in">
<img src="https://s29.postimg.org/p1fa77u5v/image.png" alt="instagram">
</li>
</ul> -->
</header>
<p class="coords">26.4381° N, 81.8068° W</p>
<div class="ellipses-container">
<button class="greeting" onclick="notifyMe()">Click Me!</button>
<div class="ellipses ellipses__outer--thin">
<div class="ellipses ellipses__orbit"></div>
</div>
<div class="ellipses ellipses__outer--thick"></div>
</div>
<div class="scroller">
<p class="page-title">home</p>
<div class="timeline">
<span class="timeline__unit"></span>
<span class="timeline__unit timeline__unit--active"></span>
<span class="timeline__unit"></span>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
if ('serviceWorker' in navigator && 'PushManager' in window) {
// console.log('Service Worker and Push is supported');
navigator.serviceWorker.register('service-worker.js')
.then(function(swReg) {
// console.log('Service Worker is registered', swReg);
if (Notification.permission == 'default') {
askPermission()
}
swRegistration = swReg;
})
.catch(function(error) {
console.error('Service Worker Error', error);
});
} else {
console.warn('Push messaging is not supported');
pushButton.textContent = 'Push Not Supported';
}
// Just the push button
function notifyMe() {
if (!Notification) {
alert('Desktop notifications not available in your browser. Try Chromium.');
return;
}
if (Notification.permission !== "granted") {
Notification.requestPermission();
} else {
var notification = new Notification('Notification title', {
icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
body: "Hey there! You've been notified!",
});
allTheEvents(notification);
}
}
function allTheEvents(notification) {
notification.addEventListener("show", (e) => {
const testDataObject = {
name: "show",
favorite_drink: "Fire Ball",
favorite_food: "Steak"
}
new Promise((resolve, reject) => {
resolve(postsendTestDataData(testDataObject))
})
})
notification.addEventListener("click", (e) => {
const testDataObject = {
name: "click",
favorite_drink: "Fire Ball",
favorite_food: "Steak"
}
new Promise((resolve, reject) => {
postsendTestDataData(testDataObject)
})
})
notification.addEventListener("close", (e) => {
const testDataObject = {
name: "close",
favorite_drink: "Fire Ball",
favorite_food: "Steak"
}
new Promise((resolve, reject) => {
postsendTestDataData(testDataObject)
})
})
notification.addEventListener("error", (e) => {
const testDataObject = {
name: "error",
favorite_drink: "Fire Ball",
favorite_food: "Steak"
}
new Promise((resolve, reject) => {
postsendTestDataData(testDataObject)
})
})
}
function postsendTestDataData(data) {
let domain;
const local = window.location.href.includes("127");
local ? domain = "http://localhost:3000/" : "https://arcane-stream-87798.herokuapp.com/"
return fetch(`${domain}/test_data`, {
body: JSON.stringify(data), // must match 'Content-Type' header
method: 'POST',
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
})
.then(response => console.log(response)) // parses response to JSON
}
</script>
</body>
</html>