Skip to content

React Native Push Notifications: OneSignal

Introduction

Introduction to OneSignal as a powerful and user-friendly solution for implementing push notifications.

Step 1: Install OneSignal React Native SDK & Link

Install the OneSignal React Native SDK by running the following command in your project directory.

npm install react-native-onesignal

npx react-native link react-native-onesignal

Step 2: Set Up OneSignal Account

  • Go to the OneSignal website and create an account if you don’t have one.
  • After logging in, create a new app and follow the instructions to set up your app on OneSignal.

Step 3: Configure OneSignal in Your App

Add it below codes in your root file.

import OneSignal from 'react-native-onesignal';

  OneSignal.setAppId('<---APP_ID--->');
    //Method for handling notifications received while app in foreground
    OneSignal.setNotificationWillShowInForegroundHandler(
      notificationReceivedEvent => {
        let notification = notificationReceivedEvent.getNotification();
        const data = notification.additionalData;
        // Complete with null means don't show a notification.
        notificationReceivedEvent.complete(notification);
      },
    );

    OneSignal.setNotificationOpenedHandler(notification => {
      console.log('OneSignal: notification opened:', notification);
    });

Step 4: Test Push Notifications

Send a test notification from the OneSignal dashboard to verify that push notifications are working in your app.

Additional Setup for iOS

Step 1: Add OneSignal to CocoaPods

Open the ios/Podfile and add the following lines:

target 'YourAppName' do
  # other pods...

  pod 'OneSignal', '>= 3.0.0', '< 4.0.0' // Add this line
end

Save the Podfile and run the following commands in the ios directory:

pod install

Step 2: Configure Info.plist

Open the ios/YourAppName/Info.plist file and add the following XML code:

<key>OneSignalAppId</key>
<string>YOUR_ONESIGNAL_APP_ID</string> // Replace YOUR_ONESIGNAL_APP_ID with OneSignal App ID.

Step 3: Code Modification

  • Open your AppDelegate.m file located in ios/YourAppName/AppDelegate.m.
  • Add the following import statement at the top:
#import <OneSignal/OneSignal.h>

Add the following method to initialize OneSignal in the didFinishLaunchingWithOptions method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // other code...

  [OneSignal initWithLaunchOptions:launchOptions
                             appId:@"YOUR_ONESIGNAL_APP_ID"
        handleNotificationReceived:^(OSNotification *notification) {
          // Handle received notification
        }
        handleNotificationAction:^(OSNotificationOpenedResult *result) {
          // Handle notification opened
        }
        settings:@{kOSSettingsKeyInAppAlerts: @NO, kOSSettingsKeyAutoPrompt: @YES}];

  // other code...
}

Step 4: Test on iOS Simulator or Device

Run your React Native app on an iOS simulator or device using the following command:

npx react-native run-ios

6 thoughts on “React Native Push Notifications: OneSignal”

  1. beatae explicabo saepe et delectus voluptatem vero dolorem laborum voluptates qui tenetur fugit quo. dignissimos nobis voluptate laudantium rerum. quasi incidunt ullam consequatur et et aut voluptate tempora odit officiis esse. dolorem eius est non voluptatem tenetur et quia nemo consectetur autem sit neque placeat. enim est quis aut veritatis ipsum consequuntur veritatis laboriosam blanditiis sed quia sed.

  2. soluta sunt velit at maxime sequi tenetur doloremque earum. magni molestiae maiores autem amet sequi harum molestiae quibusdam natus voluptatem fuga atque eos. quia saepe architecto dolor voluptates. fugiat omnis quaerat sed consequatur. qui aspernatur quia facere sed aperiam explicabo dolorum.

  3. vitae explicabo repellendus eligendi ex quia odio consequatur id dolorem. omnis qui aut dolores. neque sit amet ipsam aliquid ad esse repellendus. quod modi corporis ipsa est voluptates voluptas perspiciatis est eius vero minus delectus voluptatem asperiores. repellendus perspiciatis aspernatur et voluptatem delectus quia iure.

  4. doloremque ratione sunt eum sapiente repellat est esse ut excepturi eligendi neque suscipit accusamus quibusdam dolores iusto voluptatem quos dolores. facere est est ducimus autem cupiditate rerum quasi iusto. reiciendis repellendus ratione sed atque maiores nam eum ut nisi voluptates optio sit assumenda nesciunt et minima corporis mollitia magnam.

Leave a Reply

Your email address will not be published. Required fields are marked *