|
3 | 3 | ## How to run the project locally |
4 | 4 | - Open a new terminal session. |
5 | 5 | - CD to the root folder of the project and run `yarn` to install all Node packages. |
6 | | -- Then run `yarn start` to start a project and automatically launch a browser. |
7 | | -- Click `Subscribe Web Push` and copy a subscription object in text box. |
8 | | -- Use subscription object in a server side to send push notification. |
9 | | - |
10 | | -```sh |
11 | | - yarn create react-app web-push-api-example --template typescript |
12 | | -``` |
13 | | - |
14 | | -- Required Node.js version >= 14 |
15 | | - |
16 | | -# Get subscription object |
17 | | -- For the main project in .NET Thailand repository, it can be accessed as http://www.dotnetthailand.com/web-push-api-example/. |
| 6 | +- Then run `yarn start` to start the project and automatically launch a browser. |
| 7 | +- Click `Subscribe Web Push` and copy a subscription JSON object in a text box. |
| 8 | +- Use subscription object on a server side to send push notification. |
| 9 | +- This project requires Node.js version >= 14. |
18 | 10 |
|
19 | 11 |
|
20 | 12 | # Send push notification from a server |
21 | | -- Set a push subscrition object in `PushMessageFromServer` that we get after a user's subscribed web push. |
22 | | -- Execute the following command: |
| 13 | +- Set a push subscription object that we get after a user's subscribed web push in `PushMessageFromServer.js` |
| 14 | +- Then execute the following command: |
23 | 15 | ```sh |
24 | | - node PushMessageFromServer.js |
| 16 | + $ node PushMessageFromServer.js |
25 | 17 | ``` |
26 | 18 |
|
27 | | -## Notification does not always show as banner. |
28 | | -Turn of focus assist |
29 | | - |
30 | | -server |
31 | | -private/public key |
32 | | - |
33 | | -client |
34 | | -public key |
35 | | - |
36 | | -load register service background |
37 | | - |
38 | | -subscribe to server with public key |
39 | | -send subscription object to store on server as identity |
40 | | - |
41 | | -import web-push |
42 | | - |
43 | | -const vapIdKey = push.generateVAPIDKeys(); to get public and private keys |
44 | | -setVapidDetails() |
45 | | -sendNotification |
46 | | - |
47 | | -serviceWorker to handle receive notification |
48 | | - |
49 | | -skip waiting in application tab |
| 19 | +# Trouble shooting |
| 20 | +## Notification does not always show as banner on Windows. |
| 21 | +- Try to turn off focus assist. |
| 22 | +## No push notification message at all |
| 23 | +- You may need to manually update service worker. Open Chrome developer tool with ctrl+shift+i keys and then go to application tab. |
| 24 | + Then click Service Workers node > Skip waiting service worker. |
| 25 | + |
| 26 | +# Key concept |
| 27 | +- Generate publish and private key public and private keys with the following code |
| 28 | + ```js |
| 29 | + const vapIdKeys = webPush.generateVAPIDKeys(); |
| 30 | + const vapidKeys = webPush.generateVAPIDKeys(); |
| 31 | + console.log(vapidKeys); |
| 32 | + ``` |
| 33 | +- Use a public key in subscription script (App.tsx) |
| 34 | +- User subscribes web push with subscription script. |
| 35 | +- Use both public, private keys and subscription object on server script (PushMessageFromServer.js) to send push to a user. |
| 36 | +- Subscription object is what we store in a database. It is an identity of a subscribed user. |
| 37 | +- Use service worker script (service-worker.js) to handle push notification and customize how to show it to a user |
| 38 | +- You can test push with a localhost and do need to deploy to public URL |
| 39 | + |
| 40 | +# Get subscription object on a published website |
| 41 | +- For the main project in .NET Thailand repository, it can be accessed as http://www.dotnetthailand.com/web-push-api-example/. |
50 | 42 |
|
51 | | -Useful articles for .NET Web Push |
52 | | -- https://www.pluralsight.com/guides/html5-desktop-notifications-with-react?fbclid=IwAR1NzrQNDYSZJxKLMxWVVWes_eulWauBqa63F6Z47dDznNBL9ARiCQ26wOs |
| 43 | +# Useful articles for .NET Web Push |
| 44 | +- https://www.pluralsight.com/guides/html5-desktop-notifications-with-react |
53 | 45 | - https://blog.elmah.io/how-to-send-push-notifications-to-a-browser-in-asp-net-core/ |
54 | 46 | - https://www.bartvanuden.com/2018/01/23/push-notifications-to-your-pwa-with-asp-net-core-2-0-and-aurelia/ |
55 | 47 | - https://www.tpeczek.com/2017/12/push-notifications-and-aspnet-core-part.html |
56 | | -- https://stackoverflow.com/a/47617427/1872200 |
57 | | -- |
| 48 | +- https://stackoverflow.com/a/47617427/1872200 |
0 commit comments