项目作者: chatoo2412

项目描述 :
A simple implement of web push notifications.
高级语言: JavaScript
项目地址: git://github.com/chatoo2412/codelab-web-push-notifications.git
创建时间: 2018-03-15T15:03:08Z
项目社区:https://github.com/chatoo2412/codelab-web-push-notifications

开源协议:MIT License

下载


Web Push Notifications Codelab

A simple implement of web push notifications.

Prerequisites

  • Node.js 8+
  • One of these:
    • Google Chrome 65+
    • Firefox 59+ (enable dom.moduleScripts.enabled flag)

Install

  1. npm install

Usage

  1. npm run dev
  1. Visit http://localhost:3000/server and create a key pair.
  2. Open another tab and visit http://localhost:3000/client.
  3. Push the button and grant the permission.
  4. Return to the first page, the server, and push the send button.

How It Works

Read these first: How Push Works, Google’s codelab

Summary

Make sure you send the PushSubscription to your backend.

When your server wishes to send a push message, it makes a web push protocol request to a
push service.

When a push message is sent from a push service to a user's device, your service worker
receives a push event.

  1. Server Side
    1. Create and store application server keys.
  2. Client Side
    1. Retrieve application server public key from the server.
    2. Subscribe to the push service using application server public key.
      1. Get a permission from the user.
      2. Get a PushSubscription from the push service.
    3. Send the PushSubscription to the server.
  3. Server Side
    1. Save the PushSubscription to the DB.
    2. Generate the data that you want to send to the user.
    3. Make an API call to the push service using a library. The user’s PushSubscription and application server keys are needed.
      1. Encrypt the data with the user public key.
      2. Send the data to the endpoint URL with a payload of encrypted data.
  4. Push Service
    1. The push service routes the message to the user’s device.
  5. User’s Device
    1. User’s device wakes up the browser, which finds the correct service worker and invokes a push event.
    2. The service worker wakes up just long enough to display the notification and then goes back to sleep.

Cautions

This implement uses localStorage to share data between server and client. That’s a major - and maybe the only - difference from Google’s codelab.

  1. // client-side
  2. const updateSubscriptionOnServer = (subscription) => {
  3. // TODO: Send subscription to application server
  4. window.localStorage.setItem('subscription', JSON.stringify(subscription))
  5. }
  1. // server-side
  2. const subscription = JSON.parse(window.localStorage.getItem('subscription'))
  3. // ...
  4. const result = await webPush.sendNotification(subscription, data, options)

To Do

  • Handle exceptions: I didn’t handle exceptions on purpose to clarify my intentions.
  • Replace localStorage with a database: We have to use databases for real world applications.
  • Handle pushsubscriptionchange event: I’m still confused with this. Please help me if you can.

Documents

Basic

Advanced

Libraries and Implements