Skip to content

Commit 6e4c65a

Browse files
committed
fix: fix headers
1 parent c650907 commit 6e4c65a

File tree

7 files changed

+40
-23
lines changed

7 files changed

+40
-23
lines changed

lib/web_push_elixir.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ defmodule WebPushElixir do
108108
HTTPoison.post(endpoint, encrypted.ciphertext, %{
109109
"Authorization" => "WebPush #{signed_json_web_token}",
110110
"Content-Encoding" => "aesgcm",
111-
"Crypto-Key" => "dh=#{url_encode(vapid_public_key)};",
111+
"Crypto-Key" => "p256ecdsa=#{url_encode(vapid_public_key)}",
112112
"Encryption" => "salt=#{url_encode(encrypted.salt)}",
113-
"TTL" => "0"
113+
"TTL" => "60"
114114
})
115115
end
116116
end

lib/web_push_elixir/index.html

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@
55
<title>Web Push Elixir</title>
66
<link rel="icon" type="image/x-icon" href="favicon.ico" />
77
<link rel="manifest" href="app.webmanifest">
8+
<style>
9+
.container {
10+
display: flex;
11+
flex-direction: column;
12+
align-items: center;
13+
justify-content: center;
14+
height: 100vh;
15+
}
16+
</style>
817
</head>
918
<body>
10-
<h1>Web Push Elixir</h1>
11-
<button>Test Notification</button>
19+
<div class="container">
20+
<h1>Web Push Elixir</h1>
21+
<button>Test Notification</button>
22+
</div>
1223
<script src="main.js"></script>
1324
</body>
1425
</html>

lib/web_push_elixir/main.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ navigator.serviceWorker
77
console.error('Unable to register service worker.', err);
88
});
99

10-
document.addEventListener('DOMContentLoaded', event => {
10+
const button = document.querySelector('button');
1111

12-
const button = document.querySelector('button');
12+
button.addEventListener('click', event => {
1313

14-
button.addEventListener('click', event => {
15-
16-
Notification.requestPermission().then(permission => {
14+
Notification.requestPermission()
15+
.then(permission => {
1716

1817
navigator.serviceWorker.ready.then(registration => {
1918

2019
console.log('Service worker is active');
2120

22-
registration.showNotification('Notification Test', {
23-
body: 'Some message',
21+
registration.pushManager.subscribe({
22+
userVisibleOnly: true,
23+
applicationServerKey: "some_public_key"
24+
}).then(pushSubscription => {
25+
console.log('Received PushSubscription: ', JSON.stringify(pushSubscription));
2426
});
2527
});
2628
})
27-
});
28-
});
29+
});

lib/web_push_elixir/mock_server.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ defmodule WebPushElixir.MockServer do
3333
|> Plug.Conn.send_file(200, "./lib/web_push_elixir/service-worker.js")
3434
end
3535

36+
get "/service-worker.js" do
37+
conn
38+
|> put_resp_header("content-type", "application/x-javascript")
39+
|> Plug.Conn.send_file(200, "./lib/web_push_elixir/service-worker.js")
40+
end
41+
3642
get "/favicon.ico" do
3743
conn
3844
|> put_resp_header("content-type", "image/x-icon")

lib/web_push_elixir/service-worker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
self.addEventListener('push', event => {
22
if (event.data) {
3-
console.log('This push event has data: ', event.data.text());
3+
const promiseChain = self.registration.showNotification(event.data.text());
4+
5+
event.waitUntil(promiseChain);
46
} else {
57
console.log('This push event has no data.');
68
}

test/test_helper.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
ExUnit.start()
22

3-
DynamicSupervisor.start_child(
4-
WebPushElixir.DynamicSupervisor,
5-
{Plug.Cowboy, scheme: :http, plug: WebPushElixir.MockServer, options: [port: 4040]}
6-
)
3+
DynamicSupervisor.start_child(WebPushElixir.DynamicSupervisor,{Plug.Cowboy, scheme: :http, plug: WebPushElixir.MockServer, options: [port: 4040]})

test/web_push_elixir_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ defmodule WebPushElixirTest do
2121
assert [
2222
{"Authorization", "WebPush " <> <<_jwt::binary>>},
2323
{"Content-Encoding", "aesgcm"},
24-
{"Crypto-Key", "dh=" <> <<_vapid_public_key::binary>>},
24+
{"Crypto-Key", "p256ecdsa=" <> <<_vapid_public_key::binary>>},
2525
{"Encryption", "salt=" <> <<_salt::binary>>},
26-
{"TTL", "0"}
26+
{"TTL", "60"}
2727
] = response.request.headers
2828

2929
assert <<_body::binary>> = response.request.body
@@ -34,7 +34,7 @@ defmodule WebPushElixirTest do
3434

3535
assert [
3636
{"cache-control", "max-age=0, private, must-revalidate"},
37-
{"content-length", "348"},
37+
{"content-length", "611"},
3838
{"content-type", "text/html; charset=utf-8"},
3939
{"date", <<_date::binary>>},
4040
{"server", "Cowboy"}
@@ -58,7 +58,7 @@ defmodule WebPushElixirTest do
5858

5959
assert [
6060
{"cache-control", "max-age=0, private, must-revalidate"},
61-
{"content-length", "784"},
61+
{"content-length", "903"},
6262
{"content-type", "application/x-javascript"},
6363
{"date", <<_date::binary>>},
6464
{"server", "Cowboy"}
@@ -70,7 +70,7 @@ defmodule WebPushElixirTest do
7070

7171
assert [
7272
{"cache-control", "max-age=0, private, must-revalidate"},
73-
{"content-length", "208"},
73+
{"content-length", "262"},
7474
{"content-type", "application/x-javascript"},
7575
{"date", <<_date::binary>>},
7676
{"server", "Cowboy"}

0 commit comments

Comments
 (0)