Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-notifier

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-notifier

Fast and simple in-app notifications for React Native

  • 1.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.5K
decreased by-23.25%
Maintainers
1
Weekly downloads
 
Created
Source

react-native-notifier

npm npm bundle size platforms: ios, android, web license MIT

Fast and simple in-app notifications for React Native

Demo of package

Requirements

This library uses react-native-gesture-handler, a perfect library for swipes, and other gesture events.

If you are using react-navigation then you already have gesture-handler installed. If you don't, check Getting Started guide to install it: https://software-mansion.github.io/react-native-gesture-handler/docs/getting-started.html

Installation

yarn add react-native-notifier

Or

npm install --save react-native-notifier

Usage

Wrap your app with NotifierWrapper

import { NotifierWrapper } from 'react-native-notifier';

const App = () => (
  <NotifierWrapper>
    <Navigation />
  </NotifierWrapper>
);

Then call Notifier.showNotification() anywhere in code

import { Notifier, Easing } from 'react-native-notifier';

Notifier.showNotification({
  title: 'John Doe',
  description: 'Hello! Can you help me with notifications?',
  duration: 0,
  showAnimationDuration: 800,
  showEasing: Easing.bounce,
  onHidden: () => console.log('Hidden'),
  onPress: () => console.log('Press'),
  hideOnPress: false,
});

Or add NotifierRoot at end of your App.js component. With this approach you can show notification using reference to the NotifierRoot.

Note that NotifierRoot should be the last component to display notifications correctly. Notifier.showNotification is also available.

import { NotifierRoot } from 'react-native-notifier';

function App() {
  const notifierRef = useRef();
  return (
    <>
      <Button
        title="Show Notification"
        onPress={() => notifierRef.current?.showNotification({ title: 'Using refs' })}
      />
      <NotifierRoot ref={notifierRef} />
    </>
  );
}

All props passed to NotifierWrapper or NotifierRoot will be used as default params of showNotification function. This can be useful to set default Component param.

API

showNotification

Notifier.showNotification(params: object);

Show notification with params.

params

NameTypeDefaultDescription
titleStringnullTitle of notification. Passed to Component.
descriptionStringnullDescription of notification. Passed to Component.
durationNumber3000Time after notification will disappear. Set to 0 to not hide notification automatically
ComponentComponentNotifierComponents.NotificationComponent of the notification body. You can use one of the built-in components, or your custom component.
componentPropsObject{}Additional props that are passed to Component. See all available props of built-in components in the components section.
queueModeString'reset'Determines the order in which notifications are shown. Read more in the Queue Mode section.
swipeEnabledBooleantrueCan notification be hidden by swiping it out
animationDurationNumber300How fast notification will appear/disappear
showAnimationDurationNumberanimationDuration || 300How fast notification will appear.
hideAnimationDurationNumberanimationDuration || 300How fast notification will disappear.
easingEasingnullAnimation easing. Details: https://reactnative.dev/docs/easing
showEasingEasingeasing || nullShow Animation easing.
hideEasingEasingeasing || nullHide Animation easing.
onStartHidingFunctionnullFunction called when notification started hiding
onHiddenFunctionnullFunction called when notification completely hidden
onPressFunctionnullFunction called when user press on notification
hideOnPressBooleantrueShould notification hide when user press on it
swipePixelsToCloseNumber20How many pixels user should swipe-up notification to dismiss it
swipeEasingEasingnullAnimation easing after user finished swiping
swipeAnimationDurationNumber200How fast should be animation after user finished swiping

hideNotification

Notifier.hideNotification(onHiddenCallback?: Function);

Hide notification and run callback function when notification completely hidden.

Queue Mode

Queue mode is used to define the order in which the notification appears in case other notifications are being displayed at the moment.

For example, if you have some important information like chat messages and you want the user to see all the notifications, then you can use standby mode. Or if you want to display something like an error message, then you can use reset mode.

By default, reset mode is used, which means every new notification clears the queue and gets displayed immediately.

In most cases, you will probably use only reset or standby modes.

All possible modes:

ModeEffect
resetClear notification queue and immediately display the new notification. Used by default.
standbyAdd notification to the end of the queue.
nextPut notification in the first place in the queue. Will be shown right after the current notification disappears.
immediateSimilar to next, but also it will hide currently displayed notification.

Components

Currently, there are 2 components out of the box. If none of them fits your needs, then you can easily create your Custom Component.

NotifierComponents.Notification

Demo of Notification component

Perfect for something like chat messages and notifications like "Someone left a comment". This component is used by default.

import { Notifier, NotifierComponents } from 'react-native-notifier';

Notifier.showNotification({
  title: 'Check this image!',
  description: 'Cool, right?',
  Component: NotifierComponents.Notification,
  componentProps: {
    imageSource: require('./react.jpg'),
  },
});

Available params:

NameTypeDefaultDescription
titleStringnullTitle of notification.
descriptionStringnullDescription of notification.
componentProps.titleStyleTextStylenullThe style to use for rendering title.
componentProps.descriptionStyleTextStylenullThe style to use for rendering description.
componentProps.imageSourceObjectnullPassed to <Image /> as source param.
componentProps.imageStyleImageStylenullThe style to use for rendering image.
componentProps.ContainerComponentComponentSafeAreaViewA container of the component. Set it in case you use different SafeAreaView than the standard
componentProps.maxTitleLinesnumbernullThe maximum number of lines to use for rendering title.
componentProps.maxDescriptionLinesnumbernullThe maximum number of lines to use for rendering description.

NotifierComponents.Alert

Demo of Alert component

Perfect to use as a system alerts, like "Something went wrong" or "Operation was succeed".

import { Notifier, NotifierComponents } from 'react-native-notifier';

Notifier.showNotification({
  title: 'The request was failed',
  description: 'Check your internet connection, please',
  Component: NotifierComponents.Alert,
  componentProps: {
    alertType: 'error',
  },
});

Available params:

NameTypeDefaultDescription
titleStringnullTitle of notification.
descriptionStringnullDescription of notification.
componentProps.titleStyleTextStylenullThe style to use for rendering title.
componentProps.descriptionStyleTextStylenullThe style to use for rendering description.
componentProps.alertTypeString'success'Background color will be changed depending on the type. Available values: error(red), success(green), warn(orange) and info(blue).
componentProps.backgroundColorStringnullWhile the background of the alert depends on alertType, you can also set the other color you want.
componentProps.textColorString'white'Color of title and description.
componentProps.ContainerComponentComponentSafeAreaViewA container of the component. Set it in case you use different SafeAreaView than the standard
componentProps.maxTitleLinesnumbernullThe maximum number of lines to use for rendering title.
componentProps.maxDescriptionLinesnumbernullThe maximum number of lines to use for rendering description.

Custom Component

To customize look of the notification you can pass your own Component to showNotification function.

This makes customization much simpler than passing "style" params. With custom components you can make notification look exactly like you want.

This component will receive props title, description and anything else that you pass to componentProps object when calling showNotification.

Example

import React from 'react';
import { StyleSheet, View, Text, SafeAreaView } from 'react-native';

const styles = StyleSheet.create({
  safeArea: {
    backgroundColor: 'orange',
  },
  container: {
    padding: 20,
  },
  title: { color: 'white', fontWeight: 'bold' },
  description: { color: 'white' },
});

const CustomComponent = ({ title, description }) => (
  <SafeAreaView style={styles.safeArea}>
    <View style={styles.container}>
      <Text style={styles.title}>{title}</Text>
      <Text style={styles.description}>{description}</Text>
    </View>
  </SafeAreaView>
);

// ...

// Then show notification with the component

Notifier.showNotification({
  title: 'Custom',
  description: 'Example of custom component',
  Component: CustomComponent,
});

Demo of custom component

License

MIT

Keywords

FAQs

Package last updated on 16 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc