Git Product home page Git Product logo

react-native-local-notifications's Introduction

@guulabs/react-native-local-notifications

Schedule local notifications from React Native on iOS and Android.

Installation

yarn add @guulabs/react-native-local-notifications

Usage

import {
  scheduleNotification,
  onNotificationEvent,
  cancelScheduledNotifications,
  cancelAllScheduledNotifications,
} from '@guulabs/react-native-local-notifications';

// ...

const id = await scheduleNotification(
  {
    title: 'Title',
    body: 'This is the body of the local notification.',
  },
  {
    timestamp: Date.now() + 5000,
  }
);

// Listen for notification related events.
useEffect(() => {
  const unsubscribe = onNotificationEvent(({ type, detail }) => {
    if(type === "notificationPressed") {
      // Subscribe to this event to handle when a notification is pressed. This can be used for both background and foreground notifications.
      console.log('On Notification Pressed:', type, detail);
    } else if(type === "notificationDelivered") {
      // Subscribe to this event to handle when a notification is delivered and the app is in the foreground.
      console.log('On Notification Delivered:', type, detail);
    }
  });
  return () => {
    unsubscribe();
  }
}, []);

// Cancel a list of notification ids
await cancelScheduledNotifications([id, "another_id1", "another_id2"]);

// Cancel all
await cancelAllScheduledNotifications();

iOS

Display a notification banner for foreground notifications.

const id = await scheduleNotification(
  {
    title: 'Title',
    body: 'New',
    data: {
      key: 'value',
    },
    ios: {
      foregroundPresentationOptions: {
        banner: true,
      },
    },
  },
  {
    timestamp: Date.now() + 5000,
  }
);

Android

Set a custom icon and set an accent color.

const id = await scheduleNotification(
  {
    title: 'Title',
    body: 'This is the body of the local notification.',
    android: {
      smallIcon: 'ic_launcher',
      color: '#0000ff',
    }
  },
  {
    timestamp: Date.now() + 5000,
  }
);

API

Param name Type Default Description
title string - Title of the local notification.
body string - The body for the local notification.
ios foregroundPresentationOptions: { banner: bool } foregroundPresentationOptions: { banner: true } Show a notification banner for the notification events dispatched while the app is in foreground
android { smallIcon: string, color: string } { smallIcon: 'ic_launcher' } Use smallIcon to set a custom resource name (drawable or mipmap) for the notification icon on Android.
Use color to set a hex accent color for the notification on Android.
timestamp number - The date in milliseconds when the local notfication should be dispatched.

Should I use it?

This library was desinged to minimize the JS footprint, it contains only a few fuctions defined on the JS side. If your use case requires more than sending some simple local notifications, I strongly advive to use Notifee.

Show your support

  • ๐Ÿ‹๏ธโ€โ™‚๏ธ Follow me on Twitter @GutuCristian or LinkedIn for updates.
  • โญ๏ธ Star this repo.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

react-native-local-notifications's People

Contributors

cristiangu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.