Skip to content

Commit 1f7625a

Browse files
committed
feat: add example
1 parent 866b849 commit 1f7625a

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

lib/web_push_elixir/app.webmanifest

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Web Push Elixir",
3+
"display": "standalone"
4+
}

lib/web_push_elixir/favicon.ico

1.12 KB
Binary file not shown.

lib/web_push_elixir/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Web Push Elixir</title>
6+
<link rel="icon" type="image/x-icon" href="favicon.ico" />
7+
<link rel="manifest" href="app.webmanifest">
8+
</head>
9+
<body>
10+
<h1>Web Push Elixir</h1>
11+
<button>Click</button>
12+
<script src="main.js"></script>
13+
</body>
14+
</html>

lib/web_push_elixir/main.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
navigator.serviceWorker
2+
.register('/service-worker.js')
3+
.then(registration => {
4+
console.log('Service worker successfully registered.');
5+
})
6+
.catch(err => {
7+
console.error('Unable to register service worker.', err);
8+
});
9+
10+
document.addEventListener('DOMContentLoaded', event => {
11+
12+
const button = document.querySelector('button');
13+
14+
button.addEventListener('click', event => {
15+
16+
Notification.requestPermission().then(permission => {
17+
18+
navigator.serviceWorker.ready.then(registration => {
19+
20+
console.log('Service worker is active');
21+
22+
registration.showNotification('Notification Test', {
23+
body: 'Some message',
24+
});
25+
});
26+
})
27+
});
28+
29+
});

lib/web_push_elixir/mock_server.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,34 @@ defmodule WebPushElixir.MockServer do
88
conn
99
|> Plug.Conn.send_resp(200, "ok")
1010
end
11+
12+
get "/example" do
13+
conn
14+
|> put_resp_header("content-type", "text/html; charset=utf-8")
15+
|> Plug.Conn.send_file(200, "./lib/web_push_elixir/index.html")
16+
end
17+
18+
get "/app.webmanifest" do
19+
conn
20+
|> put_resp_header("content-type", "application/manifest+json")
21+
|> Plug.Conn.send_file(200, "./lib/web_push_elixir/app.webmanifest")
22+
end
23+
24+
get "/main.js" do
25+
conn
26+
|> put_resp_header("content-type", "application/x-javascript")
27+
|> Plug.Conn.send_file(200, "./lib/web_push_elixir/main.js")
28+
end
29+
30+
get "/service-worker.js" do
31+
conn
32+
|> put_resp_header("content-type", "application/x-javascript")
33+
|> Plug.Conn.send_file(200, "./lib/web_push_elixir/service-worker.js")
34+
end
35+
36+
get "/favicon.ico" do
37+
conn
38+
|> put_resp_header("content-type", "image/x-icon")
39+
|> Plug.Conn.send_file(200, "./lib/web_push_elixir/favicon.ico")
40+
end
1141
end

lib/web_push_elixir/service-worker.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
self.addEventListener('push', event => {
2+
if (event.data) {
3+
console.log('This push event has data: ', event.data.text());
4+
} else {
5+
console.log('This push event has no data.');
6+
}
7+
});

0 commit comments

Comments
 (0)