Skip to content

Commit ebe69c8

Browse files
committed
Add Web Push example
1 parent 62c684c commit ebe69c8

21 files changed

+469
-227
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space

.github/workflows/build-cra.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: build CRA
2+
on:
3+
push:
4+
branches:
5+
- main
6+
env:
7+
NODE_VERSION: 14.x # Set Node.js version to use
8+
9+
jobs:
10+
build:
11+
# Find more virtual environments
12+
# https://www.dotnetthailand.com/programming-cookbook/github-actions/github-actions-fundamentals#findmorevirtualenvironments
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout the latest source code from ${{ github.ref }} commit
17+
uses: actions/checkout@v2
18+
19+
- name: Use Node.js version ${{ env.NODE_VERSION }}
20+
uses: actions/setup-node@v2
21+
with:
22+
node-version: ${{ env.NODE_VERSION }}
23+
24+
- name: Build CRA to "build" folder
25+
run: |
26+
yarn install
27+
yarn build
28+
29+

PushMessageFromServer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const webpush = require('web-push');
2+
3+
// VAPID keys should be generated only once with:
4+
// const vapidKeys = webpush.generateVAPIDKeys();
5+
// console.log(vapidKeys);
6+
7+
const vapidKeys = {
8+
publicKey: 'BHG9NdYdfiCIx7xUS8u2CMhtDD-GWHb6QYuSZ908NZYZHhJEjGjcX0yTjHrWx7gDICmCEUORrLmw3uwOGBqzm2s',
9+
privateKey: 'wDf6oqOkg0SQ3FD8NU9ZNrwVZWIGBfm1gPgp6QNXWjk'
10+
};
11+
12+
webpush.setVapidDetails(
13+
'mailto:example@yourdomain.org',
14+
vapidKeys.publicKey,
15+
vapidKeys.privateKey
16+
);
17+
18+
// // This is the same output of calling JSON.stringify on a PushSubscription
19+
// const pushSubscription = {
20+
// endpoint: '.....',
21+
// keys: {
22+
// auth: '.....',
23+
// p256dh: '.....'
24+
// }
25+
// };
26+
27+
// webpush.sendNotification(pushSubscription, 'Your Push Payload Text');

README.md

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,4 @@
1-
# Getting Started with Create React App
1+
# web-push-api-example
22

3-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4-
5-
## Available Scripts
6-
7-
In the project directory, you can run:
8-
9-
### `yarn start`
10-
11-
Runs the app in the development mode.\
12-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13-
14-
The page will reload if you make edits.\
15-
You will also see any lint errors in the console.
16-
17-
### `yarn test`
18-
19-
Launches the test runner in the interactive watch mode.\
20-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21-
22-
### `yarn build`
23-
24-
Builds the app for production to the `build` folder.\
25-
It correctly bundles React in production mode and optimizes the build for the best performance.
26-
27-
The build is minified and the filenames include the hashes.\
28-
Your app is ready to be deployed!
29-
30-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31-
32-
### `yarn eject`
33-
34-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35-
36-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37-
38-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39-
40-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41-
42-
## Learn More
43-
44-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45-
46-
To learn React, check out the [React documentation](https://reactjs.org/).
3+
yarn create react-app web-push-api-example --template typescript
4+
required Node.js version >= 14

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
"react-dom": "^17.0.2",
1515
"react-scripts": "5.0.0",
1616
"typescript": "^4.4.2",
17+
"web-push": "^3.4.5",
1718
"web-vitals": "^2.1.0"
1819
},
1920
"scripts": {
2021
"start": "react-scripts start",
2122
"build": "react-scripts build",
2223
"test": "react-scripts test",
23-
"eject": "react-scripts eject"
24+
"eject": "react-scripts eject",
25+
"serve": "serve -s build"
2426
},
2527
"eslintConfig": {
2628
"extends": [
@@ -39,5 +41,8 @@
3941
"last 1 firefox version",
4042
"last 1 safari version"
4143
]
44+
},
45+
"devDependencies": {
46+
"serve": "^13.0.2"
4247
}
4348
}

public/index.html

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,11 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
5-
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
65
<meta name="viewport" content="width=device-width, initial-scale=1" />
7-
<meta name="theme-color" content="#000000" />
8-
<meta
9-
name="description"
10-
content="Web site created using create-react-app"
11-
/>
12-
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13-
<!--
14-
manifest.json provides metadata used when your web app is installed on a
15-
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16-
-->
17-
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18-
<!--
19-
Notice the use of %PUBLIC_URL% in the tags above.
20-
It will be replaced with the URL of the `public` folder during the build.
21-
Only files inside the `public` folder can be referenced from the HTML.
22-
23-
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24-
work correctly both with client-side routing and a non-root public URL.
25-
Learn how to configure a non-root public URL by running `npm run build`.
26-
-->
276
<title>React App</title>
287
</head>
298
<body>
309
<noscript>You need to enable JavaScript to run this app.</noscript>
3110
<div id="root"></div>
32-
<!--
33-
This HTML file is a template.
34-
If you open it directly in the browser, you will see an empty page.
35-
36-
You can add webfonts, meta tags, or analytics to this file.
37-
The build step will place the bundled scripts into the <body> tag.
38-
39-
To begin the development, run `npm start` or `yarn start`.
40-
To create a production bundle, use `npm run build` or `yarn build`.
41-
-->
4211
</body>
4312
</html>

public/logo192.png

-5.22 KB
Binary file not shown.

public/logo512.png

-9.44 KB
Binary file not shown.

public/manifest.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

public/service-worker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
self.addEventListener('push', () => {
2+
self.registration.showNotification('Hello world!');
3+
});

0 commit comments

Comments
 (0)