> ## Documentation Index
> Fetch the complete documentation index at: https://docs.notifly.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# React Native

> Install the Notifly React Native SDK, configure iOS and Android, and integrate push notifications.

Use the Notifly React Native SDK to integrate Notifly into your React Native app.
The SDK supports the following features:

* Register device information with Notifly to receive push notifications and display in-app popups.
  In-app popups appear only while the app is in the foreground.
* Send events and user information to Notifly for campaign targeting and analytics.
* Log user actions as events and measure campaign performance.

## Before you begin

Complete the following steps before installing the SDK.

1. **Connect your Firebase project.**\
   The Notifly iOS SDK uses [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) to send push notifications.

> [Firebase project integration guide](https://docs.notifly.tech/en/developer-guide/firebase-integration.md)

2. **Register your iOS APNs credentials.**

> [Register an APNs authentication key](https://docs.notifly.tech/en/developer-guide/firebase-integration.md#1-3-register-an-apns-authentication-key)

> [!NOTE]
>
> * For iOS builds, open the `.xcworkspace` file in Xcode.

## 1. Install the SDK

### Install the package

Install [notifly-sdk from npm](https://www.npmjs.com/package/notifly-sdk).

The badge below shows the latest version: [![npm](https://img.shields.io/npm/v/notifly-sdk.svg)](https://www.npmjs.com/package/notifly-sdk)

> [tab] npm
>
> ```shell theme={null}
> npm install notifly-sdk@latest --save
> cd ios && pod install
> ```

> [tab] yarn
>
> ```shell theme={null}
> yarn add notifly-sdk@latest
> cd ios && pod install
> ```

> [!NOTE]
>
> This adds `notifly-sdk` to `package.json`.

### Configure iOS

#### 1. Configure capabilities

1. Open `{Your_Project_Name}.xcworkspace` in Xcode. Build your app from this workspace to include the Notifly SDK.

2. Set the target's minimum iOS version to 15.0 or later. Use the same minimum version in your Podfile.

3. Enable **Push Notifications** and **Background Modes**, including **Remote notifications** and **Background fetch**.

   ![Add Capabilities](https://mintcdn.com/notifly/QWigoXWbDguqzkH4/ko/developer-guide/image/ios_setting_add_capability1.png?fit=max\&auto=format\&n=QWigoXWbDguqzkH4\&q=85\&s=53445279fef5364a29715d9a887eff48)

   ![Add Capabilities](https://mintcdn.com/notifly/umP8GQfcNYrXNUqj/ko/developer-guide/image/ios_setting_add_capability2.png?fit=max\&auto=format\&n=umP8GQfcNYrXNUqj\&q=85\&s=aa12e246d24cf9eb015ea2aef2656c09)

#### 2. Configure `AppDelegate.mm`

1. Locate the file

`AppDelegate.mm` is located at the following path:

```
ios/{ProjectName}/AppDelegate.mm
```

2. Import `notifly_sdk`

```JavaScript theme={null}
#import "notifly_sdk-Swift.h"
```

3. Add the `UNUserNotificationCenterDelegate` protocol

In `AppDelegate.h`, update the `AppDelegate` class to implement `UNUserNotificationCenterDelegate`.

> [tab] AppDelegate.h
>
> ```swift title="Header" theme={null}
> #import <RCTAppDelegate.h>
> #import <UIKit/UIKit.h>
> #import <UserNotifications/UNUserNotificationCenter.h>
>
> @interface AppDelegate : RCTAppDelegate <UNUserNotificationCenterDelegate>
>
> @end
> ```

4. Update the `AppDelegate` methods

Add the Notifly integration code to the methods in `AppDelegate.mm`.

| Method | Description |
| - | - |
| `application:didFinishLaunchingWithOptions:` | Initialize Firebase and set the notification center delegate. |
| `application:didRegisterForRemoteNotificationsWithDeviceToken:` | Register the APNs token with Notifly. |
| `application:didFailToRegisterForRemoteNotificationsWithError:` | Notify Notifly if APNs registration fails. |
| `userNotificationCenter:didReceiveNotificationResponse:` | Forward push notification click events. |
| `userNotificationCenter:willPresentNotification:` | Handle push notification display in the foreground. |

> [tab] AppDelegate.mm
>
> ```swift title="AppDelegate.mm" theme={null}
> #import <Firebase.h>
> #import <React/RCTBundleURLProvider.h>
> #import "AppDelegate.h"
> #import "notifly_sdk-Swift.h"
>
> @implementation AppDelegate
>
> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
>     /* Your Code */
>     [FIRApp configure];
>     UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
>     center.delegate = self;
>     [[UIApplication sharedApplication] registerForRemoteNotifications];
>     /* Your Code */
> }
>
> - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
>     /* Your Code */
>     [Notifly application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
>     /* Your Code */
> }
>
> - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
>     /* Your Code */
>     [Notifly application:application didFailToRegisterForRemoteNotificationsWithError:error];
>     /* Your Code */
> }
>
> - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
>     /* Your Code */
>     [Notifly userNotificationCenter:center didReceive:response];
>     completionHandler();
>     /* Your Code */
> }
>
> - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
>     /* Your Code */
>    [Notifly userNotificationCenter:center willPresent:notification withCompletionHandler:completionHandler];
>     /* Your Code */
> }
>
> /* Your Code */
> @end
> ```

### Configure an iOS Notification Service Extension (recommended)

> [!NOTE]
>
> Requires `notifly-sdk` 1.3.0 or later.

Configure a Notification Service Extension to use rich push notifications. Follow the Notifly [Push Extension guide](https://docs.notifly.tech/en/advanced/rich-push-notification.md).

#### Supported features

1. Attach images and videos to push notifications.
2. Track push notification delivery to analyze campaign performance in more detail.

> [!WARNING]
>
> The SDK works without a Notification Service Extension, but rich push notifications and delivery tracking are unavailable.

### Add initialization code

Add initialization code to your app's entry point, `index.js` or `App.js`.

```js title="index.js" highlight={2, 6} theme={null}
...
import notifly from 'notifly-sdk';

/* Your Code */

notifly.initialize({
  projectId: 'myProjectId',
  username: 'myUserName',
  // `password` is a backward-compatibility placeholder. Do not enter a real password or API Secret Key.
  password: 'myUserName',
});

/* Your Code */

AppRegistry.registerComponent(...);
```

> [!NOTE]
>
> Find `projectId` and `username` on the settings page. `password` is a placeholder for backward compatibility. Do not enter a real password or API Secret Key. Pass the same nonempty value as `username`.

***

## 2. Register user information

Notifly uses the user ID (`userId`) and user properties (`params`) to personalize marketing campaigns.\
Registered user information is used for segmentation and targeting across channels such as push notifications, email, and Kakao AlimTalk.

### 1. Set the user ID (`Notifly.setUserId()`)

Map `userId` to the identifier of the signed-in user in your app.\
Notifly uses this value to track user events and personalize campaigns.

### Parameters

**userId** `String` *required*

The signed-in user's ID. Pass `null` on sign-out to clear the association.

```js theme={null}
notifly.setUserId(userId);
```

#### Clear the user ID

On sign-out, call `setUserId(null)` to clear the association between the current device and the user.
To retain the same user data, manage it separately on your server or in your app.

> [tab] JavaScript
>
> ```JavaScript title="setUserId" theme={null}
> const handleLogin = () => {
>     ...
>     notifly.setUserId('example_user_id'); // Set the user ID.
>     ...
> }
>
> const handleLogout = () => {
>     ...
>     notifly.setUserId(); // Clear the user ID.
>     ...
> }
> ```

### 2. Set user properties

`setUserProperties` registers user properties.\
These properties are used for **segment targeting**, **message personalization**, and **delivery channel selection**.

> [!NOTE]
>
> **Before sending sensitive user properties**
>
> Before sending custom user properties that need protection, such as names or addresses, register each key under **User Management > User List > Property management** in the console and enable encryption. Use the same key names as in the data you send.
>
> For setup instructions and limitations, see the [property management guide](https://docs.notifly.tech/en/user-guide/users/properties.md).

```js theme={null}
notifly.setUserProperties(params);
```

**param** `Object` *required*

User property key-value pairs. Supports predefined keys such as `$email`, `$phone_number`, and `$line_user_id`.

> [!NOTE]
>
> * `$email` is the address for email campaigns.
> * `$phone_number` is the phone number for SMS and Kakao campaigns.
> * `$line_user_id` is the LINE user ID for LINE message campaigns.
> * You can also define custom fields such as `"plan"`, `"tier"`, and `"country"`.

> [tab] JavaScript
>
> ```js title="setUserProperties" theme={null}
> const handleRejectPushNotification = () => {
>     ...
>     notifly.setUserProperties({
>         "push_subscription_channel1": false,
>         "push_subscription_channel2": false,
>         "push_subscription_channel3": false,
>     });
>     ...
> }
> ```

***

## 3. Log events

Record user actions for **campaign triggers**, **segment conditions**, and **performance analysis**.\
For example, collect button clicks, page views, and completed purchases as events.

```js theme={null}
notifly.trackEvent(eventName, eventParams, segmentationEventParamKeys);
```

### Parameters

**eventName** `String` *required*

Event name, such as `"click_button"` or `"purchase_completed"`.

**eventParams** `Object`

Additional event parameters, such as `"plan": "premium"` and `"duration": 120`.

**segmentationEventParamKeys** `List\<String\>`

Keys to use for segmentation. Supports at most one key.

> [!NOTE]
>
> Use `segmentationEventParamKeys` to make event parameters (`eventParams`) available for targeting and related settings. You can specify **at most one key**.

> [tab] JavaScript
>
> ```js title="trackEvent" theme={null}
> const handlePurchaseTicket = () => {
>     ...
>     notifly.trackEvent(
>       "ticket_purchase",
>       {
>         "show_id": "sample_show_id",
>         "performance_start_time": 1674104659
>       },
>       ["show_id"]
>     );
>     ...
> }
>
> const handleLogin = () => {
>     ...
>     notifly.trackEvent("login");
>     ...
> }
> ```

***

## 4. Test the integration

After you install the SDK, follow [Test your integration](https://docs.notifly.tech/en/integration-test.md) to verify event collection and push notification delivery.

## 5. Advanced integration

> [card] [Request push notification permission](https://docs.notifly.tech/en/advanced/push-notification-consent.md)

> [card] [Subscribe to push notification click events](https://docs.notifly.tech/en/advanced/push-notification-click-events.md)

> [card] [In-app popup event listener](https://docs.notifly.tech/en/advanced/inapp-popup-event-listener.md)

## FAQ

> [accordion] Can I use the SDK with an existing Firebase Cloud Messaging integration?
>
> The Notifly Android SDK can coexist with Firebase Cloud Messaging already used in your app.

> [accordion] The build reports a Notifly class error in AppDelegate.mm.
>
> Add `use_frameworks! :linkage => :static` to your Podfile to try static linking. If `use_frameworks!` already exists, add only the `:linkage => :static` option.

> [accordion] Push notifications do not appear on iOS while the app is in the foreground.
>
> If you already use Firebase Messaging, configure `firebase.json` as described below.
>
> Following the [React Native Firebase foreground presentation options guide](https://rnfirebase.io/messaging/usage#foreground-presentation-options-ios), add the following code to `firebase.json`.
>
> ```json theme={null}
> {
>   "react-native": {
>     "messaging_ios_foreground_presentation_options": [
>       "badge",
>       "sound",
>       "list",
>       "banner"
>     ]
>   }
> }
> ```

> [accordion] The compiler cannot find `notifly_sdk-Swift.h`.
>
> * The iOS `notifly_sdk` is written in Swift. During the build, Xcode generates the Swift compatibility header `notifly_sdk-Swift.h` to expose it to Objective-C. Add `"${PODS_CONFIGURATION_BUILD_DIR}"` to **Header Search Paths** in the build settings for your target.
> * [Import Swift into Objective-C](https://developer.apple.com/documentation/swift/importing-swift-into-objective-c)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.