React Native OneSignal
React Native Push Notifications support with OneSignal integration.
![npm downloads](https://img.shields.io/npm/dm/react-native-onesignal.svg?style=flat-square)
Installation
npm install react-native-onesignal
Android Installation
In your AndroidManifest.xml
.....
<permission
android:name="${applicationId}.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<application ....>
<activity
android:launchMode="singleTop">
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
.....
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!
![Add Files To...](http://i.imgur.com/puxHiIg.png)
![Library Imported Successfuly](http://i.imgur.com/YJPQLPD.png)
- Ensure that
libRTCOneSignal.a
is linked through Link Binary With Libraries
on Build Phases
:
![Add Files To...](http://i.imgur.com/IxIQ4s8.png)
Adding the Code
- When you reach
AppDelegate.m
instructions on the OneSignal documentation, stop and enter this following code snippets instead:
-
Import RCTOneSignal.h
:
#import "RCTOneSignal.h"
-
Synthesize oneSignal
after @implementation AppDelegate
@synthesize oneSignal = _oneSignal;
-
On the application didFinishLaunchingWithOptions
method, insert the following code (replace YOUR_ONESIGNAL_APP_ID with your OneSignal app ID):
self.oneSignal = [[RCTOneSignal alloc] initWithLaunchOptions:launchOptions
appId:@"YOUR_ONESIGNAL_APP_ID"];
-
After application
insert the code for the notification event:
* (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification {
[RCTOneSignal didReceiveRemoteNotification:notification];
}
- You're All Set!
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(recievedTags);
});
OneSignal.deleteTag(tag);
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);
}
});
});
};
TODO