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:

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"
        }
      ]
    ]
  }
}

Configuring app extensions with Expo

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
        }
      ]
    ]
  }
}

Lastly, add the bundle identifier for this app extension to your project’s credentials configuration: <your-app-bundle-id>.BrazeExpoRichPush. For further details on this process, refer to Using app extensions with Expo Application Services.

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"
        }
      ]
    ]
  }
}

Lastly, add the bundle identifier for this app extension to your project’s credentials configuration: <your-app-bundle-id>.BrazeExpoPushStories. For further details on this process, refer to Using app extensions with Expo Application Services.

Using app extensions with Expo Application Services

If you are using Expo Application Services (EAS) and have enabled enableBrazeIosRichPush or enableBrazeIosPushStories, you will need to declare the corresponding bundle identifiers for each app extension in your project. There are multiple ways you can approach this step, depending on how your project is configured to manage code signing with EAS.

One approach is to use the appExtensions configuration in your app.json file by following Expo’s app extensions documentation. Alternatively, you can set up the multitarget setting in your credentials.json file by following Expo’s local credentials documentation.

HOW HELPFUL WAS THIS PAGE?
New Stuff!