Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include backend, split into versions
- Loading branch information
Showing
27 changed files
with
540 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
google/ | ||
private.key | ||
priv* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
# push-notifications | ||
Frontend code for subscribing to and receiving push notifications in the browser | ||
|
||
**Demo project for [c't Magazin](https://ct.de/) in March 2018** | ||
**Demo project for [c't Magazin](https://ct.de/) in April 2018** | ||
|
||
This code borrows from [Google's web push code lab](https://developers.google.com/web/fundamentals/codelabs/push-notifications/) and uses the [Push companion](https://web-push-codelab.glitch.me/) as backend. | ||
This shows several phases of building an own web push client. | ||
|
||
- Phase 1: show notifications | ||
- Phase 2: register a service worker | ||
- Phase 3: send messages from dev tools | ||
- Phase 4: send messages from [Push companion](https://web-push-codelab.glitch.me/) | ||
- Phase 5: build your own subscription service based on PHP/MySQL and send messages from the command shell using [web-push](https://github.com/web-push-libs/web-push). The included bash script creates a key pair and requires a shell with OpenSSL, dd, and tr (can be installed in Windows with [Cygwin](https://www.cygwin.com/)) | ||
- Phase 6: create a more elegant solution (not included) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
body { | ||
font-family: Verdana, Arial, sans-serif; | ||
width: 50em; | ||
margin: auto; | ||
text-align: center; | ||
} | ||
|
||
label { | ||
display: inline-block; | ||
margin-bottom: 1em; | ||
text-align: left; | ||
} | ||
|
||
label > input, button { | ||
margin: .25em 0 1em; | ||
} | ||
|
||
input, button { | ||
padding: .5em; | ||
line-height: 1.5em; | ||
font-size: large; | ||
} | ||
|
||
input, output, button { | ||
display: block; | ||
margin: 1em auto; | ||
} | ||
|
||
input:not([type="submit"]), output { | ||
display: block; | ||
width: 50em; | ||
text-align: left; | ||
font-family: monospace; | ||
font-size: 11px; | ||
} | ||
|
||
output { | ||
max-width: 80em; | ||
padding: 2em; | ||
white-space: pre-wrap; | ||
word-wrap: break-word; | ||
background-color: #ccc; | ||
} | ||
|
||
button { | ||
width: 20em; | ||
margin: 3em auto; | ||
padding: .5em; | ||
font-size: large; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!doctype html> | ||
<html lang="de"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="author" content="Herbert Braun"> | ||
<title>Einfache Benachrichtigungen</title> | ||
<link rel="stylesheet" href="../common/less-ugly.css"> | ||
<script src="notification.js" defer></script> | ||
</head> | ||
<body> | ||
<h1>Einfache Benachrichtigungen</h1> | ||
<form action="#"> | ||
<textarea></textarea> | ||
<button>Benachrichtigung senden</button> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
document.querySelector('button').addEventListener('click', ev => { | ||
ev.preventDefault(); | ||
if (!('Notification' in window)) { | ||
throw new Error('Der Browser unterstützt keine Benachrichtigungen.'); | ||
return; | ||
} | ||
const ask = Notification.requestPermission(permission => { | ||
if (permission !== 'granted') { | ||
alert('Keine Erlaubnis zum Anzeigen von Benachrichtigungen!'); | ||
return; | ||
} | ||
const txt = document.querySelector('textarea').value; | ||
if (txt.match(/^\W*$/)) { | ||
alert('Bitte geben Sie Text ein.'); | ||
return; | ||
} | ||
setTimeout(() => { | ||
const msg = new Notification('Dummy-Nachricht', { | ||
body: txt, | ||
lang: 'de', | ||
icon: '../common/ct.png', | ||
image: '../common/bild.jpg' | ||
}); | ||
msg.onclick = ev => alert('Sie haben die Nachricht angeklickt!'); | ||
msg.onerror = err => console.error(err); | ||
msg.onshow = ev => console.info(ev); | ||
msg.onclose = ev => console.info(ev); | ||
}, 1000); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
const btnTexts = [ | ||
'Abonnieren', | ||
'Abo beenden' | ||
]; | ||
const btn = document.querySelector('button'); | ||
let worker = null; | ||
let isSubscribed = false; | ||
if ('PushManager' in window) { | ||
navigator.serviceWorker.register('worker.js') | ||
.then(reg => { | ||
worker = reg; | ||
btn.disabled = false; | ||
worker.pushManager.getSubscription() | ||
.then(subscription => { | ||
if (subscription === null) { | ||
btn.textContent = btnTexts[0]; | ||
} else { | ||
isSubscribed = true; | ||
btn.textContent = btnTexts[1]; | ||
} | ||
}); | ||
}) | ||
.catch(err => console.error(err)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
'use strict'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!doctype html> | ||
<html lang="de"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="author" content="Herbert Braun"> | ||
<title>Push-Benachrichtigungen</title> | ||
<link rel="stylesheet" href="../common/less-ugly.css"> | ||
<script src="push.js" defer></script> | ||
</head> | ||
<body> | ||
<h1>Push-Benachrichtigungen</h1> | ||
<form action="#"> | ||
<label> | ||
Public Key: | ||
<input type="text"> | ||
</label> | ||
<button disabled>Push-Nachrichten nicht möglich</button> | ||
<output></output> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
const btnTexts = [ | ||
'Abonnieren', | ||
'Abo beenden' | ||
]; | ||
const btn = document.querySelector('button'); | ||
let worker = null; | ||
let isSubscribed = false; | ||
if ('PushManager' in window) { | ||
navigator.serviceWorker.register('worker.js') | ||
.then(reg => { | ||
worker = reg; | ||
btn.disabled = false; | ||
worker.pushManager.getSubscription() | ||
.then(subscription => { | ||
if (subscription === null) { | ||
btn.textContent = btnTexts[0]; | ||
} else { | ||
isSubscribed = true; | ||
btn.textContent = btnTexts[1]; | ||
} | ||
}); | ||
}) | ||
.catch(err => console.error(err)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
self.addEventListener('push', ev => { | ||
const title = 'Nachricht für dich!'; | ||
let text = 'Text ...'; | ||
if (ev.data !== null) | ||
text = ev.data.text(); | ||
ev.waitUntil(self.registration.showNotification(title, {body: text})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!doctype html> | ||
<html lang="de"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="author" content="Herbert Braun"> | ||
<title>Push-Benachrichtigungen</title> | ||
<link rel="stylesheet" href="../common/less-ugly.css"> | ||
<script src="push.js" defer></script> | ||
</head> | ||
<body> | ||
<h1>Push-Benachrichtigungen</h1> | ||
<form action="#"> | ||
<label> | ||
Public Key: | ||
<input type="text"> | ||
</label> | ||
<button disabled>Push-Nachrichten nicht möglich</button> | ||
<output></output> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[{000214A0-0000-0000-C000-000000000046}] | ||
Prop3=19,11 | ||
[InternetShortcut] | ||
IDList= | ||
URL=https://web-push-codelab.glitch.me/ |
Oops, something went wrong.