Skip to content

Commit 6839ba5

Browse files
committed
Build for relative path
1 parent 5c82835 commit 6839ba5

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

PushMessageFromServer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ webpush.setVapidDetails(
1515
vapidKeys.privateKey
1616
);
1717

18-
// // This is the same output of calling JSON.stringify on a PushSubscription
19-
// const pushSubscription = {
18+
// This is the same output of calling JSON.stringify on a PushSubscription
19+
20+
// const pushSubscription =
2021
// endpoint: '.....',
2122
// keys: {
2223
// auth: '.....',
2324
// p256dh: '.....'
2425
// }
2526
// };
2627

27-
// webpush.sendNotification(pushSubscription, 'Your Push Payload Text');
28+
const pushSubscription =
29+
const notification = { title: `Hello at ${(new Date()).toISOString()}` };
30+
31+
32+
webpush.sendNotification(pushSubscription,);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@
4444
},
4545
"devDependencies": {
4646
"serve": "^13.0.2"
47-
}
47+
},
48+
"homepage": "."
4849
}

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1" />
66
<title>React App</title>
7+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
78
</head>
89
<body>
910
<noscript>You need to enable JavaScript to run this app.</noscript>

public/service-worker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
self.addEventListener('push', () => {
2-
self.registration.showNotification('Hello world!');
1+
self.addEventListener('push', (event) => {
2+
const data = event.data.json();
3+
event.waitUntil(
4+
self.registration.showNotification(data.title, {
5+
body: 'Buzz! Buzz!',
6+
vibrate: [300, 100, 400], // Vibrate 300ms, pause 100ms, then vibrate 400ms
7+
icon: './favicon.ico',
8+
tag: 'vibration-sample'
9+
})
10+
);
311
});

src/App.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { useEffect } from 'react';
1+
import { useEffect, useState } from 'react';
22
import './App.css';
33

44
export default function App() {
5+
const [subscription, setSubscription] = useState('');
56
useEffect(() => {
67
window.addEventListener('load', async () => {
78
const sw = await navigator.serviceWorker.register('./service-worker.js');
@@ -17,14 +18,22 @@ export default function App() {
1718
userVisibleOnly: true,
1819
applicationServerKey: publicKey,
1920
});
20-
console.log(`push subscription :\n\n${JSON.stringify(push)}`);
21+
console.log(`push subscription :\n\n${JSON.stringify(push, null, 2)}`);
22+
setSubscription(JSON.stringify(push, null, 2));
2123
}
2224

25+
const style = {
26+
width: '100%'
27+
};
28+
2329
return (
2430
<div>
2531
<button onClick={handleSubscribeWebPush}>
2632
Subscript web push
2733
</button>
34+
<div>
35+
<textarea rows={10} style={style} value={subscription} />
36+
</div>
2837
</div>
2938
);
3039
}

0 commit comments

Comments
 (0)