React Native OneSignal SDK
The React Native OneSignal SDK facilitates the integration of OneSignal's push notification, in-app messaging, email, and SMS services into iOS and Android applications built with React Native. The current stable version is 5.4.3. This library is actively maintained with frequent minor and patch releases, often addressing native SDK updates, bug fixes, and new features like the recent support for React Native's New Architecture (TurboModules) introduced in v5.4.0. Key differentiators include its comprehensive cross-platform support for various notification types and its user-centric APIs for managing user data across channels, distinguishing it from lower-level notification APIs provided by mobile OS platforms.
Common errors
-
OneSignal: notification will show in foreground: { is and is not a valid Notification object }cause Incorrectly accessing properties on the `notificationReceivedEvent` object in the `setNotificationWillShowInForegroundHandler` callback.fixAccess the `Notification` object using `notificationReceivedEvent.getNotification()` and then its properties (e.g., `notification.body`). The `notificationReceivedEvent` itself is not the `Notification` object directly. -
Error: `react-native-onesignal` requires `react-native` version `>=0.76.0`.
cause Mismatch between the installed `react-native-onesignal` version and the `react-native` peer dependency requirement.fixUpdate your `react-native` package to a compatible version (e.g., `>=0.76.0`) or downgrade `react-native-onesignal` to a version that supports your current `react-native` version. Check the `package.json` for specific peer dependency ranges. -
Error: Could not find `OneSignal.h` or `OneSignal.m` for iOS build.
cause Common CocoaPods or native module linking issue, often after upgrading React Native or `react-native-onesignal`, or when clearing `node_modules` and not reinstalling pods.fixNavigate to your `ios` directory and run `pod install`. Ensure `OneSignal` is correctly linked in your `Podfile`. Clean your Xcode build folder (`Product > Clean Build Folder`) and delete derived data. -
Build failed due to 'Namespace declaration' in AndroidManifest.xml after updating.
cause Migration of Android namespace from AndroidManifest.xml to build.gradle, which was addressed in older versions like 5.3.4.fixEnsure you are on `react-native-onesignal@5.3.4` or newer. Manually move the `namespace` declaration from `AndroidManifest.xml` to your `app/build.gradle` file, if it still exists there, following modern Android Gradle Plugin conventions.
Warnings
- breaking Version 5.4.0 introduced support for React Native's New Architecture (TurboModules). Old architecture may not work correctly anymore. Existing projects must update their builds and clean old build artifacts.
- deprecated The `LiveActivities.Exit` method was deprecated in version 5.4.2 as it is currently unsupported by the underlying native SDKs.
- gotcha OneSignal's new user-centric APIs and v5.x.x SDKs offer an improved user and data management experience but may not have 1:1 feature parity with previous versions. Migrating existing apps requires careful testing.
- gotcha Strict CodeGen rules for React Native and Expo apps can prevent builds, as fixed in version 5.4.3.
- breaking Update Android SDK from 5.7.6 to 5.7.7 in v5.4.2 includes a downgrade of Kotlin from 2.2.0 to 1.9.25. This could cause build issues if your project explicitly uses or relies on Kotlin 2.2.0 or newer.
Install
-
npm install react-native-onesignal -
yarn add react-native-onesignal -
pnpm add react-native-onesignal
Imports
- OneSignal
const OneSignal = require('react-native-onesignal');import OneSignal from 'react-native-onesignal';
- NotificationWillDisplayEvent, Notification
import { NotificationWillDisplayEvent, Notification } from 'react-native-onesignal';import type { NotificationWillDisplayEvent, Notification } from 'react-native-onesignal'; - InAppMessageClickEvent
import { InAppMessageClickEvent } from 'react-native-onesignal';import type { InAppMessageClickEvent } from 'react-native-onesignal';
Quickstart
import React, { useEffect } from 'react';
import { View, Text, Alert, Platform } from 'react-native';
import OneSignal from 'react-native-onesignal';
const ONESIGNAL_APP_ID = process.env.ONESIGNAL_APP_ID ?? 'YOUR_ONESIGNAL_APP_ID';
const App = () => {
useEffect(() => {
// OneSignal Init Code
OneSignal.setAppId(ONESIGNAL_APP_ID);
// Method for setting the External User Id
OneSignal.setExternalUserId('your-user-id-from-your-system');
// Method for handling notifications received while app in foreground
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
console.log("OneSignal: notification will show in foreground: ", notificationReceivedEvent);
let notification = notificationReceivedEvent.get ;();
Alert.alert("Notification Received", notification.body || '');
notificationReceivedEvent.complete(notification);
});
// Method for handling notification clicks
OneSignal.setNotificationOpenedHandler(notification => {
console.log("OneSignal: notification opened: ", notification);
Alert.alert("Notification Clicked", notification.notification.body || '');
});
// Prompt for push notifications (iOS) and Android 13+ permission
if (Platform.OS === 'ios' || (Platform.OS === 'android' && parseFloat(Platform.Version) >= 33)) {
OneSignal.promptForPushNotificationsWithProvisionalNotification();
}
return () => {
// Clean up listeners if necessary
};
}, []);
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>OneSignal React Native Example</Text>
<Text>Check console for OneSignal logs</Text>
</View>
);
};
export default App;