1.5.4 • Published 5 months ago

react-push-notification v1.5.4

Weekly downloads
648
License
MIT
Repository
github
Last release
5 months ago

npm version


react-push-notification

Easy, type-safe, & lightweight push notification library for React.js. Written in TypeScript & compiled to JavaScript for robust code.

In-app notification system, as well as web native Notification support.

npm.io npm.io

Install

yarn add react-push-notification

or

npm i react-push-notification

Sneakpeak

In-app notification example. Regular React components.

npm.io

Web native notification example. Web native components. Send push notifications outside of the browser while the browser is running in the background or just idle.

Mac OSX example:

npm.io npm.io

Set-up

Add the notifications component to the top of your React.js project. This is probably index.js or app.js. When using native: true, this step is not required.

import { Notifications } from 'react-push-notification';

const App = () => {
    return (
      <div className="app">
        // Top of DOM tree
        <Notifications />
        <div className="row">
          <div className="content">
           Hello world.
          </div>
        </div>
      </div>
    );
  }
};

export default App;

Usage

import the addNotification function and call it.

import addNotification from 'react-push-notification';

const Page = () => {

    const buttonClick = () => {
        addNotification({
            title: 'Warning',
            subtitle: 'This is a subtitle',
            message: 'This is a very long message',
            theme: 'darkblue',
            native: true // when using native, your OS will handle theming.
        });
    };

    return (
      <div className="page">
          <button onClick={buttonClick} className="button">
           Hello world.
          </button>
      </div>
    );
  }
};

export default Page;

Props

PropertyDescription
position stringOne of top-left, top-middle, top-right, bottom-left, bottom-middle, bottom-right.Default: top-left

addNotification({Options}) argument properties

The addNotification() function has the following function type:

const options = {
    title: 'title',
    subtitle: 'subtitle', //optional
    message: 'message', //optional
    onClick: (e: Event | Notification) => void, //optional, onClick callback.
    theme: 'red', //optional, default: undefined
    duration: 3000, //optional, default: 5000,
    backgroundTop: 'green', //optional, background color of top container.
    backgroundBottom: 'darkgreen', //optional, background color of bottom container.
    colorTop: 'green', //optional, font color of top container.
    colorBottom: 'darkgreen', //optional, font color of bottom container.
    closeButton: 'Go away', //optional, text or html/jsx element for close text. Default: Close,
    native?: boolean, //optional, makes the push notification a native OS notification
    icon?: string, // optional, Native only. Sets an icon for the notification.
    vibrate?: number | number[], // optional, Native only. Sets a vibration for the notification.
    silent?: boolean // optional, Native only. Makes the notification silent.

};

const addNotification: (options: Options) => void;

The addNotification() function takes an object as argument with the follow properties:

PropertyDescription
title stringRequired. Title of the push notification
subtitle stringOptional. Subtitle of the push notification
message stringOptional. Message of the push notification
onClick (e: Event OR Notification) => voidOptional. onClick callback of push notification.When native: true e will be of type Notification.Else e will be of type Event.
theme stringOptional. One of darkblue, red, light, undefined.Default: undefined
duration numberOptional. Duration of the push notification in ms.Default: 3000
backgroundTop stringOptional. background color of top container.
backgroundBottom stringOptional. background color of bottom container.
colorTop stringOptional. font color of top container.
colorBottom stringOptional. font color of bottom container.
closeButton stringOptional. text or html/jsx element for close text.Default: Close
native booleanOptional. Turns the notification into a native web notification. Default: false
icon stringOptional. Native only. Shows an icon in the notification.
vibrate numbernumber[]Optional. Native only. Makes the notification vibrate.
silent booleanOptional. Native only. Makes the notification silent.

The custom background or font colors will always override a chosen theme.

Changelog

v1.3.0

Added native OS push notification support, as well as an onClick callback function.

1.5.4

5 months ago

1.5.3

1 year ago

1.5.2

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.3.2

1 year ago

1.4.0

1 year ago

1.3.1

1 year ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.9

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

1.0.0

4 years ago