1.0.14 • Published 4 years ago

use-pwa-hook v1.0.14

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

usePWA React hook

Creates an easy way of interacting with service-worker funtionality within a React based progressive web application

Support for Safari push notifications on OSX is not yet developed.

Getting Started

Usage:

This hook assumes a service worker is available with a filename of sw.js

Environment variable to support push notification subscriptions as public VAPID key expected with name:

VAPID_PUBLIC

A sample service worker is provided with the package.

  import { usePWA } from 'use-pwa-hook';
  .....
  const {
      onAskPushPermission,
      onSubscribePush,
      onPushUnsubscribe,
      pushSupported,
      pushConsent,
      userPushSubscription,
      onInstall,
      canInstall,
      error,
      loading
    } = usePWA();

Examples of using this hook:

  const handleSubscribe = async () => {
    const subscription = await onSubscribePush();
    // ... Post notification subscription id to backend etc for later use..
  };

  const handleAskPermission = async () => {
    if (pushSupported) {
      // Request push notifications permission, returns a boolean
      const permission = await onAskPushPermission();
      permission && (await handleSubscribe());
    } else {
      // ... handle push unsupported here
    }
  };

  const handleUnsubscribe = async () => {
    const success = await onPushUnsubscribe();
    // ... handle clearing push subscription for user etc
  };

  const handleOnInstall = () => {
    // Prompt the user to install your app if eligible
    canInstall && onInstall()
  };

Links

NPM

GitHub

Contact

Any questions contact chaycarnell@gmail.com

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.9

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago