Convert Figma logo to code with AI

OneSignal logoreact-native-onesignal

React Native Library for OneSignal Push Notifications Service

1,564
373
1,564
97

Top Related Projects

1,806

⚛️ A feature rich notifications library for React Native.

React Native Local and Remote Notifications

React Native Notifications

React Native module for CodePush

Quick Overview

React Native OneSignal is a library that integrates OneSignal's push notification service into React Native applications. It provides a seamless way to implement push notifications, in-app messaging, and other engagement features in cross-platform mobile apps built with React Native.

Pros

  • Easy integration with React Native projects
  • Cross-platform support for iOS and Android
  • Comprehensive documentation and community support
  • Rich feature set including push notifications, in-app messaging, and analytics

Cons

  • Dependency on OneSignal's backend services
  • Potential impact on app size due to additional SDK
  • Learning curve for developers new to push notification systems
  • May require additional configuration for certain advanced features

Code Examples

  1. Initializing OneSignal:
import OneSignal from 'react-native-onesignal';

OneSignal.setAppId('YOUR_ONESIGNAL_APP_ID');
  1. Handling notification opened events:
OneSignal.setNotificationOpenedHandler(notification => {
  console.log("OneSignal: notification opened:", notification);
});
  1. Sending a tag to OneSignal:
OneSignal.sendTag("key", "value");
  1. Prompting for push notification permissions (iOS):
OneSignal.promptForPushNotificationsWithUserResponse(response => {
  console.log("Prompt response:", response);
});

Getting Started

  1. Install the package:

    npm install react-native-onesignal
    
  2. For iOS, install pods:

    cd ios && pod install
    
  3. Initialize OneSignal in your app's entry file (e.g., App.js):

    import OneSignal from 'react-native-onesignal';
    
    OneSignal.setAppId('YOUR_ONESIGNAL_APP_ID');
    OneSignal.promptForPushNotificationsWithUserResponse();
    
    // Optional: Add notification opened handler
    OneSignal.setNotificationOpenedHandler(notification => {
      console.log("OneSignal: notification opened:", notification);
    });
    
  4. For Android, ensure you have the required permissions in your AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET" />
    

Remember to replace 'YOUR_ONESIGNAL_APP_ID' with your actual OneSignal App ID. Refer to the official documentation for more detailed setup instructions and advanced configurations.

Competitor Comparisons

1,806

⚛️ A feature rich notifications library for React Native.

Pros of Notifee

  • More flexible and customizable notification handling
  • Better support for background and scheduled notifications
  • Actively maintained with frequent updates

Cons of Notifee

  • Steeper learning curve due to more complex API
  • Requires more setup and configuration compared to OneSignal
  • Limited built-in analytics and user segmentation features

Code Comparison

Notifee:

import notifee from '@notifee/react-native';

await notifee.requestPermission();
await notifee.displayNotification({
  title: 'Notification Title',
  body: 'Notification Body',
});

react-native-onesignal:

import OneSignal from 'react-native-onesignal';

OneSignal.setAppId('YOUR_ONESIGNAL_APP_ID');
OneSignal.promptForPushNotificationsWithUserResponse();
OneSignal.postNotification({
  contents: { en: 'Notification Body' },
  headings: { en: 'Notification Title' },
});

Notifee offers more granular control over notification display and behavior, while OneSignal provides a simpler API for basic push notifications. Notifee is better suited for complex notification requirements, whereas OneSignal excels in ease of use and quick implementation for standard push notification scenarios.

React Native Local and Remote Notifications

Pros of react-native-push-notification

  • More flexible and customizable for local notifications
  • Supports a wider range of Android notification features
  • Lighter weight and focused solely on push notifications

Cons of react-native-push-notification

  • Requires more setup and configuration
  • Less comprehensive analytics and user segmentation
  • Limited cross-platform consistency in some features

Code Comparison

react-native-push-notification:

PushNotification.localNotification({
  title: "My Notification Title",
  message: "My Notification Message",
  playSound: true,
  soundName: "default",
});

react-native-onesignal:

OneSignal.postNotification({
  contents: { en: "My Notification Message" },
  headings: { en: "My Notification Title" },
  ios_sound: "default",
  android_sound: "default",
});

Both libraries offer similar functionality for sending notifications, but react-native-onesignal provides a more unified API across platforms. react-native-push-notification offers more granular control over local notifications, while react-native-onesignal simplifies the process of sending notifications from a server.

react-native-push-notification is better suited for projects requiring extensive customization of local notifications, while react-native-onesignal excels in scenarios where cross-platform consistency and advanced analytics are priorities. The choice between the two depends on specific project requirements and the desired balance between flexibility and ease of use.

React Native Notifications

Pros of react-native-notifications

  • More lightweight and focused solely on notifications, potentially offering better performance
  • Provides greater flexibility and customization options for developers
  • Actively maintained by Wix, a company with a strong presence in the React Native ecosystem

Cons of react-native-notifications

  • Requires more setup and configuration compared to react-native-onesignal
  • May lack some advanced features provided by OneSignal's full-service platform
  • Limited to push notifications, while OneSignal offers additional messaging channels

Code Comparison

react-native-notifications:

import { Notifications } from 'react-native-notifications';

Notifications.registerRemoteNotifications();
Notifications.events().registerNotificationReceivedForeground((notification, completion) => {
  console.log(`Notification received in foreground: ${notification.title}`);
  completion({alert: false, sound: false, badge: false});
});

react-native-onesignal:

import OneSignal from 'react-native-onesignal';

OneSignal.setAppId('YOUR_ONESIGNAL_APP_ID');
OneSignal.promptForPushNotificationsWithUserResponse(response => {
  console.log('Prompt response:', response);
});
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
  console.log('OneSignal: notification will show in foreground:', notificationReceivedEvent);
});

React Native module for CodePush

Pros of react-native-code-push

  • Enables over-the-air updates for React Native apps without going through app stores
  • Supports both iOS and Android platforms
  • Allows for easy rollbacks and phased deployments

Cons of react-native-code-push

  • Limited to JavaScript and asset updates; native code changes require full app updates
  • Requires additional setup and configuration compared to push notification services
  • May face potential app store policy restrictions in some cases

Code comparison

react-native-code-push:

import codePush from "react-native-code-push";

const App = () => {
  // Your app code
};

export default codePush(App);

react-native-onesignal:

import OneSignal from 'react-native-onesignal';

OneSignal.setAppId('YOUR_ONESIGNAL_APP_ID');
OneSignal.promptForPushNotificationsWithUserResponse();

Summary

react-native-code-push focuses on enabling over-the-air updates for React Native apps, allowing developers to push updates directly to users without going through app stores. It offers flexibility in deployment strategies but is limited to JavaScript and asset updates.

react-native-onesignal, on the other hand, specializes in push notifications and user engagement features. It provides a simpler setup process for implementing push notifications but doesn't offer the same level of app update capabilities as react-native-code-push.

The choice between these libraries depends on the specific needs of your project, whether you prioritize easy app updates or robust push notification features.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React Native OneSignal SDK

npm version npm downloads


⚠️ Migration Advisory for current OneSignal customers

Our new user-centric APIs and v5.x.x SDKs offer an improved user and data management experience. However, they may not be at 1:1 feature parity with our previous versions yet.

If you are migrating an existing app, we suggest using iOS and Android’s Phased Rollout capabilities to ensure that there are no unexpected issues or edge cases. Here is the documentation for each:

If you run into any challenges or have concerns, please contact our support team at support@onesignal.com


OneSignal is a free email, sms, push notification, and in-app message service for mobile apps. This SDK makes it easy to integrate your native React-Native iOS and/or Android apps with OneSignal.

Installation

See the Setup Guide for setup instructions.

Change Log

See this repository's release tags for a complete change log of every released version.

Support

Please visit this repository's Github issue tracker for feature requests and bug reports related specifically to the SDK. For account issues and support please contact OneSignal support from the OneSignal.com dashboard.

Demo Project

To make things easier, we have published demo projects in the /examples folder of this repository.

NPM DownloadsLast 30 Days