React Native Local and Remote Notifications
React Native Local and Remote Notifications
React Native Local and Remote Notifications for iOS and Android
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.
Check out for changes and migration in the CHANGELOG:
Maintainers are welcome ! Feel free to contact me :wink:
Changelog is available from version 3.1.3 here: Changelog
npm install --save react-native-push-notificationyarn add react-native-push-notificationNOTE: 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.
Having a problem? Read the troubleshooting guide before raising an issue.
The component uses PushNotificationIOS for the iOS part. You should follow their installation instructions.
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
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).
<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):
@Override
public void onNewIntent(Intent intent) {
...
super.onNewIntent(intent);
...
}Make sure you have installed setup Firebase correctly.
In android/build.gradle
buildscript {
...
dependencies {
...
classpath('com.google.gms:google-services:4.3.3')
...
}
}In android/app/build.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-coreis 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.
In android/settings.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
dependencies {
...
implementation project(':react-native-push-notification')
...
}Manually register module in MainApplication.java (if you did not use react-native link):
…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, usuallyindex.js.
…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.
When any notification is opened or received the callback onNotification is called passing an object with the notification data.
Notification object example:
{
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
}PushNotification.localNotification(details: Object)EXAMPLE:
…PushNotification.localNotificationSchedule(details: Object)EXAMPLE:
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.
});PushNotification.popInitialNotification(callback)EXAMPLE:
PushNotification.popInitialNotification((notification) => {
console.log('Initial Notification', notification);
});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'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
<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:
<meta-data
android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />AndroidManifest: <meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="..." />fcm_fallback_notification_channelYou can list available channels with:
PushNotification.getChannels(function (channel_ids) {
console.log(channel_ids); // ['channel_id_1']
});You can check if a channel exists with:
PushNotification.channelExists(channel_id, function (exists) {
console.log(exists); // true/false
});You can check if a channel blocked with:
PushNotification.channelBlocked(channel_id, function (blocked) {
console.log(blocked); // true/false
});You can delete a channel with:
PushNotification.deleteChannel(channel_id);The id parameter for PushNotification.localNotification is required for this operation. The id supplied will then be used for the cancel operation.
PushNotification.localNotification({
...
id: '123'
...
});
PushNotification.cancelLocalNotification('123');PushNotification.cancelAllLocalNotifications()Cancels all scheduled notifications AND clears the notifications alerts that are in the notification centre.
PushNotification.removeAllDeliveredNotifications();Remove all delivered notifications from Notification Center
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.PushNotification.removeDeliveredNotifications(identifiers);Removes the specified notifications from Notification Center
Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
| identifiers | array | Yes | Array of notification identifiers. |
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 |
|---|
Group notifications
[Android] Not support new arch react native 0.76.5
Schedule Daily Notification is not working after few days if i did not open the app
update build gradle version
ongoing?: boolean | undefined; Why this property doesnt work, i can skip notifications using this.
Issue on Android : FirebaseMessaging.getInstance().getToken() and FirebaseMessaging.getInstance().deleteToken();
Push Notifications not working above React Native 0.75
[IOS] When import PushNotifications i get a error new NativeEventEmitter() and cant open my app
How to use local push only
Get onNotification triggered when a remote notification is received