Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-native-push-notification

> 前端框架
Open source

React Native Local and Remote Notifications

6.8K stars0 likes2 views
WebsiteGitHub

About

React Native Local and Remote Notifications

React Native Push Notifications

React Native Local and Remote Notifications for iOS and Android

State of the repository

This repository is not actively maintained. The main reason is time. The second one is probably the complexity of notifications on both iOS and Android. Since this project probably need a huge refactor to fix some issue or to implement new features. I think you should probably consider these alternatives: Notifee free since september or react-native-notifications.

If you are interested in being a maintainer of this project, feel free to ask in issues.

Version 7.x is live !

Check out for changes and migration in the CHANGELOG:

Changelog

Supporting the project

Maintainers are welcome ! Feel free to contact me :wink:

Changelog

Changelog is available from version 3.1.3 here: Changelog

Installation

NPM

bash
npm install --save react-native-push-notification

Yarn

bash
yarn add react-native-push-notification

NOTE: If you target iOS you also need to follow the installation instructions for PushNotificationIOS since this package depends on it.

NOTE: For Android, you will still have to manually update the AndroidManifest.xml (as below) in order to use Scheduled Notifications.

Issues

Having a problem? Read the troubleshooting guide before raising an issue.

Pull Requests

Please read...

iOS manual Installation

The component uses PushNotificationIOS for the iOS part. You should follow their installation instructions.

Android manual Installation

NOTE: firebase-messaging, prior to version 15 requires to have the same version number in order to work correctly at build time and at run time. To use a specific version:

In your android/build.gradle

gradle
ext {
    googlePlayServicesVersion = "<Your play services version>" // default: "+"
    firebaseMessagingVersion = "<Your Firebase version>" // default: "21.1.0"

    // Other settings
    compileSdkVersion = <Your compile SDK version> // default: 23
    buildToolsVersion = "<Your build tools version>" // default: "23.0.1"
    targetSdkVersion = <Your target SDK version> // default: 23
    supportLibVersion = "<Your support lib version>" // default: 23.1.1
}

NOTE: localNotification() works without changes in the application part, while localNotificationSchedule() only works with these changes:

In your android/app/src/main/AndroidManifest.xml

…

If not using a built in Android color (@android:color/{name}) for the notification_color meta-data item. In android/app/src/main/res/values/colors.xml (Create the file if it doesn't exist).

xml
<resources>
    <color name="white">#FFF</color>
</resources>

If your app has an @Override on onNewIntent in MainActivity.java ensure that function includes a super call on onNewIntent (if your MainActivity.java does not have an @Override for onNewIntent skip this):

java
    @Override
    public void onNewIntent(Intent intent) {
        ...
        super.onNewIntent(intent);
        ...
    }

If you use remote notifications

Make sure you have installed setup Firebase correctly.

In android/build.gradle

gradle

buildscript {
    ...
    dependencies {
        ...
        classpath('com.google.gms:google-services:4.3.3')
        ...
    }
}

In android/app/build.gradle

gradle
dependencies {
  ...
  implementation 'com.google.firebase:firebase-analytics:17.3.0'
  ...
}

apply plugin: 'com.google.gms.google-services'

Then put your google-services.json in android/app/.

Note: firebase/release-notes

The Firebase Android library firebase-core is no longer needed. This SDK included the Firebase SDK for Google Analytics.

Now, to use Analytics or any Firebase product that recommends the use of Analytics (see table below), you need to explicitly add the Analytics dependency: com.google.firebase:firebase-analytics:17.3.0.

If you don't use autolink

In android/settings.gradle

gradle
...
include ':react-native-push-notification'
project(':react-native-push-notification').projectDir = file('../node_modules/react-native-push-notification/android')

In your android/app/build.gradle

gradle
 dependencies {
    ...
    implementation project(':react-native-push-notification')
    ...
 }

Manually register module in MainApplication.java (if you did not use react-native link):

…

Usage

DO NOT USE .configure() INSIDE A COMPONENT, EVEN App

If you do, notification handlers will not fire, because they are not loaded. Instead, use .configure() in the app's first file, usually index.js.

…

Example app

Example folder contains an example app to demonstrate how to use this package. The notification Handling is done in NotifService.js.

Please test your PRs with this example app before submitting them. It'll help maintaining this repo.

Handling Notifications

When any notification is opened or received the callback onNotification is called passing an object with the notification data.

Notification object example:

javascript
{
    foreground: false, // BOOLEAN: If the notification was received in foreground or not
    userInteraction: false, // BOOLEAN: If the notification was opened by the user from the notification area or not
    message: 'My Notification Message', // STRING: The notification message
    data: {}, // OBJECT: The push data or the defined userInfo in local notifications
}

Local Notifications

javascript
PushNotification.localNotification(details: Object)

EXAMPLE:

…

Scheduled Notifications

javascript
PushNotification.localNotificationSchedule(details: Object)

EXAMPLE:

javascript
PushNotification.localNotificationSchedule({
  //... You can use all the options from localNotifications
  message: "My Notification Message", // (required)
  date: new Date(Date.now() + 60 * 1000), // in 60 secs
  allowWhileIdle: false, // (optional) set notification to work while on doze, default: false

  /* Android Only Properties */
  repeatTime: 1, // (optional) Increment of configured repeatType. Check 'Repeating Notifications' section for more info.
});

Get the initial notification

javascript
PushNotification.popInitialNotification(callback)

EXAMPLE:

javascript
PushNotification.popInitialNotification((notification) => {
  console.log('Initial Notification', notification);
});

Custom sounds

In android, add your custom sound file to [project_root]/android/app/src/main/res/raw

In iOS, add your custom sound file to the project Resources in xCode.

In the location notification json specify the full file name:

soundName: 'my_sound.mp3'

Channel Management (Android)

To use channels, create them at startup and pass the matching channelId through to PushNotification.localNotification or PushNotification.localNotificationSchedule.

…

NOTE: Without channel, notifications don't work

In the notifications options, you must provide a channel id with channelId: "your-channel-id", if the channel doesn't exist the notification might not be triggered. Once the channel is created, the channel cannot be updated. Make sure your channelId is different if you change these options. If you have created a channel in another way, it will apply options of the channel.

If you want to use a different default channel for remote notification, refer to the documentation of Firebase:

Set up a Firebase Cloud Messaging client app on Android

xml
  <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="@string/default_notification_channel_id" />

For local notifications, the same kind of option is available:

  • you can use:
    xml
      <meta-data
          android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
          android:value="@string/default_notification_channel_id" />
  • If not defined, fallback to the Firebase value defined in the AndroidManifest:
    xml
      <meta-data
          android:name="com.google.firebase.messaging.default_notification_channel_id"
          android:value="..." />
  • If not defined, fallback to the default Firebase channel id fcm_fallback_notification_channel

List channels

You can list available channels with:

javascript
PushNotification.getChannels(function (channel_ids) {
  console.log(channel_ids); // ['channel_id_1']
});

Channel exists

You can check if a channel exists with:

javascript
PushNotification.channelExists(channel_id, function (exists) {
  console.log(exists); // true/false
});

Channel blocked

You can check if a channel blocked with:

javascript
PushNotification.channelBlocked(channel_id, function (blocked) {
  console.log(blocked); // true/false
});

Delete channel

You can delete a channel with:

javascript
PushNotification.deleteChannel(channel_id);

Cancelling notifications

1) cancelLocalNotification

The id parameter for PushNotification.localNotification is required for this operation. The id supplied will then be used for the cancel operation.

javascript
PushNotification.localNotification({
    ...
    id: '123'
    ...
});
PushNotification.cancelLocalNotification('123');

2) cancelAllLocalNotifications

javascript
PushNotification.cancelAllLocalNotifications()

Cancels all scheduled notifications AND clears the notifications alerts that are in the notification centre.

3) removeAllDeliveredNotifications

javascript
PushNotification.removeAllDeliveredNotifications();

Remove all delivered notifications from Notification Center

4) getDeliveredNotifications

javascript
PushNotification.getDeliveredNotifications(callback);

Provides you with a list of the app’s notifications that are still displayed in Notification Center

Parameters:

Name Type Required Description
callback function Yes Function which receive an array of delivered notifications.

A delivered notification is an object containing:

  • identifier : The identifier of this notification.
  • title : The title of this notification.
  • body : The body of this notification.
  • category : The category of this notification (optional).
  • userInfo : An object containing additional notification data (optional).
  • thread-id : The thread identifier of this notification, if has one.

5) removeDeliveredNotifications

javascript
PushNotification.removeDeliveredNotifications(identifiers);

Removes the specified notifications from Notification Center

Parameters:

Name Type Required Description
identifiers array Yes Array of notification identifiers.

6) getScheduledLocalNotifications

javascript
PushNotification.getScheduledLocalNotifications(callback);

Provides you with a list of the app’s scheduled local notifications that are yet to be displayed

Parameters:

Name Type Required Description

Issues· 159 open

View all issuesOpen on GitHub
  • #1591

    Group notifications

    Updated Jan 10, 2025
  • #2432

    [Android] Not support new arch react native 0.76.5

    Updated Jan 9, 2025
  • #2400

    Schedule Daily Notification is not working after few days if i did not open the app

    StaleUpdated Jan 2, 2025
  • #2398

    update build gradle version

    StaleUpdated Dec 20, 2024
  • #2431

    ongoing?: boolean | undefined; Why this property doesnt work, i can skip notifications using this.

    Updated Dec 11, 2024
  • #2154

    Issue on Android : FirebaseMessaging.getInstance().getToken() and FirebaseMessaging.getInstance().deleteToken();

    StaleUpdated Dec 4, 2024
  • #2429

    Push Notifications not working above React Native 0.75

    Updated Nov 28, 2024
  • #2326

    [IOS] When import PushNotifications i get a error new NativeEventEmitter() and cant open my app

    Updated Nov 12, 2024
  • #2412

    How to use local push only

    Updated Nov 8, 2024
  • #2402

    Get onNotification triggered when a remote notification is received

    Updated Oct 23, 2024

> Tags

Javaandroidiospush-notificationsreact-native

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 23, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架