React Native OneSignal
React Native Push Notifications support with OneSignal integration.
Installation
npm install react-native-onesignal
Android Installation
In your AndroidManifest.xml
.....
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application ....>
<activity
android:launchMode="singleTop">
.....
In android/settings.gradle
...
include ':react-native-onesignal'
project(':react-native-onesignal').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-onesignal/android')
In android/app/build.gradle
...
android {
...
defaultConfig {
...
manifestPlaceholders = [manifestApplicationId: "${applicationId}",
onesignal_app_id: "YOUR_ONESIGNAL_ID",
onesignal_google_project_number: "YOUR_GOOGLE_PROJECT_NUMBER"]
}
}
dependencies {
...
compile project(':react-native-onesignal')
}
Register module (in MainActivity.java
)
import com.geektime.reactnativeonesignal.ReactNativeOneSignalPackage;
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
...
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativeOneSignalPackage(this)
);
}
......
}
iOS Installation
Importing The Library
- Drag the file
RCTOneSignal.xcodeproj
from /node_modules/react-native-onesignal/ios
into the Libraries
group in the Project navigator. Ensure that Copy items if needed
is UNCHECKED!
- Ensure that
libRTCOneSignal.a
is linked through Link Binary With Libraries
on Build Phases
:
- Ensure that
Header Search Paths
on Build Settings
has the path $(SRCROOT)/../node_modules/react-native-onesignal
set to recursive
:
Adding the Code
Android Usage
In your index.android.js
:
import OneSignal from 'react-native-onesignal';
OneSignal.configure({
onNotificationOpened: function(message, data, isActive) {
console.log('MESSAGE: ', message);
console.log('DATA: ', data);
console.log('ISACTIVE: ', isActive);
}
});
iOS Usage
In iOS, we have to wait a little bit before fetching the notification. The reason is that notification is coming too fast, before the main view of the app is being rendered.
Therefore, the notification could get lost. We solve it in an ugly way, but working one.
In your index.ios.js
:
import OneSignal from 'react-native-onesignal';
var pendingNotifications = [];
OneSignal.configure({
onNotificationOpened: function(message, data, isActive) {
var notification = {message: message, data: data, isActive: isActive};
console.log('NOTIFICATION OPENED: ', notification);
pendingNotifications.push(notification);
handleNotification(notification);
}
});
Handling Notifications
When any notification is opened or received the callback onNotification
is called passing an object with the notification data.
Notification object example:
{
isActive: false,
message: 'My Notification Message',
data: {},
}
Sending and Getting OneSignal Tags
We exposed the tags API of OneSignal (currently on Android) in order to segment people in a better way.
OneSignal.sendTags(missingTags);
OneSignal.getTags((receivedTags) => {
console.log(receivedTags);
});
OneSignal.deleteTag(tag);
Getting Player ID and Push Token
We exposed the idsAvailable API of OneSignal (both Android & iOS) as a callback so you can handle it further yourself.
OneSignal.idsAvailable((idsAvailable) => {
console.log(idsAvailable.pushToken);
console.log(idsAvailable.userId);
});
The following example is from our own App and needs to be customized in order to work.
Example:
_syncOneSignal = () => {
var allTags = {};
var missingTags = {};
OneSignal.getTags((receivedTags) => {
for (var i = this.categories.length * 1; i >= 0; i--) {
var category = this.categories[i];
if (!(category.slug in receivedTags)) {
missingTags[category.slug] = category.is_push_default;
}
allTags[category.slug] = category.is_push_default;
};
if (Object.keys(missingTags).length > 0) {
OneSignal.sendTags(missingTags);
}
Object.keys(receivedTags).forEach(function(key,index) {
if (!(key in allTags)) {
OneSignal.deleteTag(key);
}
});
});
};
CREDITS
Thanks for all the awesome fellows that contributed to this repository!
@danpe, @lunchieapp, @gaykov, @williamrijksen, @adrienbrault, @kennym, @dunghuynh, @holmesal, @joshuapinter
TODO