Skip to content

Push notification integration

This reference article covers how to set push notifications for React Native. Integrating push notifications requires setting up each native platform separately. Follow the respective guides listed to finish the installation.

Step 1: Complete the initial setup

Set the enableBrazeIosPush and enableFirebaseCloudMessaging options in your app.json file to enable push for iOS and Android, respectively. Refer to the configuration instructions here for more details.

Note that you will need to use these settings instead of the native setup instructions if you are depending on additional push notification libraries like Expo Notifications.

Step 1.1: Register for push

Register for push using Google’s Firebase Cloud Messaging (FCM) API. For a full walkthrough, refer to the following steps from the Native Android push integration guide:

  1. Add Firebase to your project.
  2. Add Cloud Messaging to your dependencies.
  3. Create a service account.
  4. Generate JSON credentials.
  5. Upload your JSON credentials to Braze.

Step 1.2: Add your Google Sender ID

First, go to Firebase Console, open your project, then select  Settings > Project settings.

The Firebase project with the "Settings" menu open.

Select Cloud Messaging, then under Firebase Cloud Messaging API (V1), copy the Sender ID to your clipboard.

The Firebase project's "Cloud Messaging" page with the "Sender ID" highlighted.

Next, open your project’s app.json file and set your firebaseCloudMessagingSenderId property to the Sender ID in your clipboard. For example:

1
"firebaseCloudMessagingSenderId": "693679403398"

Step 1.3: Add the path to your Google Services JSON

In your project’s app.json file, add the path to your google-services.json file. This file is required when setting enableFirebaseCloudMessaging: true in your configuration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "expo": {
    "android": {
      "googleServicesFile": "PATH_TO_GOOGLE_SERVICES"
    },
    "plugins": [
      [
        "@braze/expo-plugin",
        {
          "androidApiKey": "YOUR-ANDROID-API-KEY",
          "iosApiKey": "YOUR-IOS-API-KEY",
          "enableBrazeIosPush": true,
          "enableFirebaseCloudMessaging": true,
          "firebaseCloudMessagingSenderId": "YOUR-FCM-SENDER-ID",
          "androidHandlePushDeepLinksAutomatically": true
        }
      ],
    ]
  }
}

Step 1.1: Upload APNs certificates

Generate an Apple Push Notification service (APNs) certificate and uploaded it to the Braze dashboard. For a full walkthrough, see Uploading your APNs certificate.

Step 1.2: Choose an integration method

If you don’t plan on requesting push permissions when the app launched, omit the requestAuthorizationWithOptions:completionHandler: call in your AppDelegate, then skip to Step 2. Otherwise, follow the native iOS integration guide.

When you’re finished, continue to Step 1.3.

Step 1.3: Migrate your push key

If you were previously using expo-notifications to manage your push key, run expo fetch:ios:certs from your application’s root folder. This will download your push key (a .p8 file), which can then be uploaded to the Braze dashboard.

Step 2: Request push notifications permission

Use the Braze.requestPushPermission() method (available on v1.38.0 and up) to request permission for push notifications from the user on iOS and Android 13+. For Android 12 and below, this method is a no-op.

This method takes in a required parameter that specifies which permissions the SDK should request from the user on iOS. These options have no effect on Android.

1
2
3
4
5
6
7
8
const permissionOptions = {
  alert: true,
  sound: true,
  badge: true,
  provisional: false
};

Braze.requestPushPermission(permissionOptions);

Step 2.1: Listen for push notifications (optional)

You can additionally subscribe to events where Braze has detected and handled an incoming push notification. Use the listener key Braze.Events.PUSH_NOTIFICATION_EVENT.

1
2
3
4
Braze.addListener(Braze.Events.PUSH_NOTIFICATION_EVENT, data => {
  console.log(`Push Notification event of type ${data.payload_type} seen. Title ${data.title}\n and deeplink ${data.url}`);
  console.log(JSON.stringify(data, undefined, 2));
});

Push notification event fields

For a full list of push notification fields, refer to the table below:

Field Name Type Description
payload_type String Specifies the notification payload type. The two values that are sent from the Braze React Native SDK are push_opened and push_received. Only push_opened events are supported on iOS.
url String Specifies the URL that was opened by the notification.
use_webview Boolean If true, URL will open in-app in a modal webview. If false, the URL will open in the device browser.
title String Represents the title of the notification.
body String Represents the body or content text of the notification.
summary_text String Represents the summary text of the notification. This is mapped from subtitle on iOS.
badge_count Number Represents the badge count of the notification.
timestamp Number Represents the time at which the payload was received by the application.
is_silent Boolean If true, the payload is received silently. For details on sending Android silent push notifications, refer to Silent push notifications on Android. For details on sending iOS silent push notifications, refer to Silent push notifications on iOS.
is_braze_internal Boolean This will be true if a notification payload was sent for an internal SDK feature, such as geofences sync, Feature Flag sync, or uninstall tracking. The payload is received silently for the user.
image_url String Specifies the URL associated with the notification image.
braze_properties Object Represents Braze properties associated with the campaign (key-value pairs).
ios Object Represents iOS-specific fields.
android Object Represents Android-specific fields.

Step 3: Enable deep linking (optional)

To enable Braze to handle deep links inside React components when a push notification is clicked, follow the additional steps.

Our BrazeProject sample app contains a complete example of implemented deep links. To learn more about what deep links are, see our FAQ article.

For Android, setting up deep links is identical to setting up deep links on native Android apps. If you want the Braze SDK to handle push deep links automatically, set androidHandlePushDeepLinksAutomatically: true in your app.json.

Step 3.1: Add deep linking capabilities

For iOS, add populateInitialUrlFromLaunchOptions to your AppDelegate’s didFinishLaunchingWithOptions method. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.moduleName = @"BrazeProject";
  self.initialProps = @{};

  BRZConfiguration *configuration = [[BRZConfiguration alloc] initWithApiKey:apiKey endpoint:endpoint];
  configuration.triggerMinimumTimeInterval = 1;
  configuration.logger.level = BRZLoggerLevelInfo;
  Braze *braze = [BrazeReactBridge initBraze:configuration];
  AppDelegate.braze = braze;

  [self registerForPushNotifications];
  [[BrazeReactUtils sharedInstance] populateInitialUrlFromLaunchOptions:launchOptions];

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

Use the Linking.getInitialURL() method for deep links that open your app, and the Braze.getInitialURL method for deep links inside push notifications that open your app when it isn’t running. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Linking.getInitialURL()
  .then(url => {
    if (url) {
      console.log('Linking.getInitialURL is ' + url);
      showToast('Linking.getInitialURL is ' + url);
      handleOpenUrl({ url });
    }
  })
  .catch(err => console.error('Error getting initial URL', err));

// Handles deep links when an iOS app is launched from a hard close via push click.
Braze.getInitialURL(url => {
  if (url) {
    console.log('Braze.getInitialURL is ' + url);
    showToast('Braze.getInitialURL is ' + url);
    handleOpenUrl({ url });
  }
});

Step 4: Test displaying push notifications

At this point, you should be able to send notifications to the devices. Adhere to the following steps to test your push integration.

  1. Set an active user in the React application by calling Braze.changeUserId('your-user-id') method.
  2. Head to Campaigns and create a new push notification campaign. Choose the platforms that you’d like to test.
  3. Compose your test notification and head over to the Test tab. Add the same user-id as the test user and click Send Test. You should receive the notification on your device shortly.

A Braze push campaign showing you can add your own user ID as a test recipient to test your push notification.

Forwarding Android push to additional FMS

If you want to use an additional Firebase Messaging Service (FMS), you can specify a fallback FMS to call if your application receives a push that isn’t from Braze. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "expo": {
    "plugins": [
      [
        "@braze/expo-plugin",
        {
          ...
          "androidFirebaseMessagingFallbackServiceEnabled": true,
          "androidFirebaseMessagingFallbackServiceClasspath": "com.company.OurFirebaseMessagingService"
        }
      ]
    ]
  }
}

Enabling rich push notifications for iOS

To enable rich push notifications on iOS using Expo, configure the enableBrazeIosRichPush property to true in your expo.plugins object in app.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "expo": {
    "plugins": [
      [
        "@braze/expo-plugin",
        {
          ...
          "enableBrazeIosRichPush": true
        }
      ]
    ]
  }
}

Enabling push stories for iOS

To enable push stories on iOS using Expo, ensure you have an app group defined for your application. For more information, see Adding an App Group.

Next, configure the enableBrazeIosPushStories property to true and assign your app group ID to iosPushStoryAppGroup in your expo.plugins object in app.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "expo": {
    "plugins": [
      [
        "@braze/expo-plugin",
        {
          ...
          "enableBrazeIosPushStories": true,
          "iosPushStoryAppGroup": "group.com.company.myApp.PushStories"
        }
      ]
    ]
  }
}
HOW HELPFUL WAS THIS PAGE?
New Stuff!