Fork, do not use
React Native Push Notifications
React Native Local and Remote Notifications for iOS and Android
Supported React Native Versions
Component Version | RN Versions | README |
---|---|---|
1.0.7 | <= 0.27 | Open |
1.0.8 | 0.28 | Open |
2.0.1 | 0.29 | Open |
2.0.2 | 0.30, 0.31, 0.32 | Open |
>= 2.1.0 | >= 0.33 | Open |
Installation
npm install react-native-push-notification
iOS Installation
The component uses PushNotificationIOS for the iOS part.
Please see: PushNotificationIOS
Android Installation
NOTE: To use a specific play-service-gcm
version, use in your android/app/build.gradle
(change 8.1.0
for your version):
... dependencies { ... compile project(':react-native-push-notification') compile ('com.google.android.gms:play-services-gcm:8.1.0') { force = true; }}
In your AndroidManifest.xml
..... .....
In android/settings.gradle
... include ':react-native-push-notification'project(':react-native-push-notification').projectDir = file('../node_modules/react-native-push-notification/android')
Register module (in MainApplication.java
)
// <--- Import Package
Usage
var PushNotification = ; PushNotification;
Handling Notifications
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
Local Notifications
PushNotification.localNotification(details: Object)
EXAMPLE:
PushNotification;
Scheduled Notifications
PushNotification.localNotificationSchedule(details: Object)
EXAMPLE:
PushNotification;
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'
Cancelling notifications
1) cancelLocalNotifications
PushNotification.cancelLocalNotifications(details);
The the details
parameter allows you to specify a userInfo
dictionary that can be used to match one or more scheduled notifications. Each
matched notification is cancelled and its alerts removed from the notification centre. The RN docs suggest this is an optional parameter, but
it is not.
PushNotification;
2) cancelAllLocalNotifications
PushNotification.cancelAllLocalNotifications()
Cancels all scheduled notifications AND clears the notifications alerts that are in the notification centre.
NOTE: there is currently no api for removing specific notification alerts from the notification centre.
Set application badge icon
PushNotification.setApplicationIconBadgeNumber(number: number)
Works natively in iOS.
Uses the ShortcutBadger on Android, and as such with not work on all Android devices.
Sending Notification Data From Server
Same parameters as PushNotification.localNotification()
iOS Only Methods
PushNotification.checkPermissions(callback: Function)
Check permissions
PushNotification.getApplicationIconBadgeNumber(callback: Function)
get badge number
PushNotification.abandonPermissions()
Abandon permissions