Skip to content

Phone alerts

Turn Web Push on for this browser, see devices that have it, turn one off and send a test.

<div class="cap-web-push" id="push-devices">
  <div class="cap-web-push-live" role="status"><div class="cap-web-push-note">Test alert sent to each device.</div></div>
  <div class="cap-web-push-actions">
    <button type="button" class="cap-btn" data-variant="primary">Turn on for this device</button>
    <button type="button" class="cap-btn">Send a test alert</button>
  </div>
  <ul class="cap-web-push-devices" aria-label="Devices with alerts on">
    <li class="cap-web-push-device">
      <span class="cap-web-push-device-name">Chrome on Android <span class="cap-web-push-device-detail">turned on 3 days ago, last alert an hour ago</span></span>
      <button type="button" class="cap-btn">Turn off<span class="cap-sr-only"> Chrome on Android</span></button>
    </li>
    <li class="cap-web-push-device">
      <span class="cap-web-push-device-name">Safari on iOS <span class="cap-web-push-device-detail">turned on a week ago, no alert yet</span></span>
      <button type="button" class="cap-btn">Turn off<span class="cap-sr-only"> Safari on iOS</span></button>
    </li>
  </ul>
</div>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/web-push.css";
import { pushSupported, deviceLabel, subscribePush } from "capsomer/behavior/web-push";

Usage

<div class="cap-push">
  <div class="cap-push-note" role="status">Phone alerts are on for this device.</div>   <!-- data-tone="crit" and role="alert" for a failure -->
  <div class="cap-push-actions">
    <button type="button" class="cap-btn" data-variant="primary">Turn on for this device</button>
    <button type="button" class="cap-btn">Send a test alert</button>
  </div>
  <ul class="cap-push-devices" aria-label="Devices with alerts on">
    <li class="cap-push-device">
      <span class="cap-push-device-name">Chrome on Android <span class="cap-push-device-detail">turned on 3 days ago</span></span>
      <button type="button" class="cap-btn">Turn off</button>
    </li>
  </ul>
</div>

Examples

Turning on failed

<div class="cap-web-push">
  <div class="cap-web-push-note" data-tone="crit" role="alert">This browser did not allow notifications. Allow them for this site in the browser's settings, then try again.</div>
  <div class="cap-web-push-actions">
    <button type="button" class="cap-btn" data-variant="primary">Turn on for this device</button>
    <button type="button" class="cap-btn" disabled>Send a test alert</button>
  </div>
</div>

Not set up, and a browser with no Web Push

<div class="cap-web-push" id="push-setup">
  <div class="cap-web-push-live" role="status"><div class="cap-web-push-note" data-tone="warn">Not set up yet: the VAPID key is not set.</div></div>
  <div class="cap-web-push-actions">
    <span class="cap-web-push-unsupported">This browser cannot receive Web Push.</span>
    <button type="button" class="cap-btn" disabled>Send a test alert</button>
  </div>
</div>

API reference

PushDevices

PropTypeRequiredDoes
publicKeystring | nullYesThe server's VAPID public key; null while it is not set up.
workerstringYesThe worker to register (push-sw.js as the site serves it) and its scope.
scopestringYes
devicesPushDevice[]Yes
busybooleanA request is in flight: the controls wait.
notePushNote | nullThe result of the last change, from the app (or set here when turning on fails).
problemReactNodeWhat the setup still needs, in the server's words.
onTurnOn(subscription: PushSubscriptionJSON, label: string) => Promise<void> | voidYesSend the new subscription to the server.
onTurnOff(id: PushDevice["id"]) => voidYes
onTest() => voidYes

Functions

FunctionSignatureDoes
pushSupported(): boolean
deviceLabel(ua: string): string"Chrome on Android": enough to tell two devices apart, and nothing more.
subscribePush(opts: { worker: string; scope: string; publicKey: string; }): Promise<{ subscription: PushSubscriptionJSON; label: string; }>Asks for permission, registers the worker and subscribes (or returns the subscription this browser already has). Throws PushRefused when the person or the browser said no.

Parts and data attributes

ClassIs
.cap-web-pushthe root
.cap-web-push-actionsa part
.cap-web-push-devicea part
.cap-web-push-device-detaila part
.cap-web-push-device-namea part
.cap-web-push-devicesa part
.cap-web-push-livea part
.cap-web-push-notea part
.cap-web-push-unsupporteda part
AttributeValues
data-tonecrit warn
StateRead from
:emptythe platform

Accessibility

KeyDoes
TabMoves through Turn on, Send a test alert, then each device's Turn off
Enter, SpacePresses the button
  • A failure is an alert; a result is a status, so it is read when it changes.
  • The device list is a named list.
  • Buttons wait (disabled) while a request is in flight.