Getting started: Push Notifications in React Native

Open in

This guide will get you started with Ably Push Notifications in a new React Native application.

You'll learn how to set up your application with Firebase Cloud Messaging (FCM), register devices with Ably, send push notifications, subscribe to channel-based push, and handle incoming notifications on both iOS and Android.

Prerequisites

  1. Sign up for an Ably account.
  2. Create a new app, and create your first API key in the API Keys tab of the dashboard.
  • Your API key needs the publish, subscribe capabilities.
  • Also add the push-admin capability if you're using the same API key to send a push notification. In production this would more likely be a server using a different API key.
  1. Add a rule to a channel so you can test sending push notification via a channel. Select Rules in the Ably dashboard, add a new rule and enable the Push notifications option.
  2. Install Node.js 18 or higher.
  3. Set up your React Native development environment following the React Native CLI Quickstart.
  4. For iOS: Install Xcode. Push notifications require a physical iOS device (simulators do not support push).
  5. For Android: Install Android Studio. Use a physical device or an emulator with Google Play Services installed.

(Optional) Install Ably CLI

Use the Ably CLI as an additional client to quickly test Pub/Sub features and push notifications.

  1. Install the Ably CLI:
npm install -g @ably/cli
  1. Run the following to log in to your Ably account and set the default app and API key:
ably login

Set up Firebase Cloud Messaging

Firebase Cloud Messaging delivers push notifications for both Android and iOS. To enable FCM:

  1. Go to the Firebase Console and create a new project (or use an existing one).
  2. Register your Android app using your package name. Download google-services.json and place it in android/app/.
  3. Download your Firebase service account JSON file from your Firebase console: Project configuration → Service Accounts → Generate new private key.
  4. In the Ably dashboard left sidebar, navigate to your app's Push Notifications.
  5. Scroll to the Configure push service for devices section and press Configure Push.
  6. Upload your Firebase service account JSON file in Setting up Firebase Cloud Messaging section.
  7. In the Apple Developer portal, go to Certificates, Identifiers & Profiles → Keys.
  8. Add a new key and check Apple Push Notifications service (APNs), click Register.
  9. Download the .p8 file — you can only download it once. Note your Key ID and Team ID.
  10. In the Firebase Console, go to Project configuration → Cloud Messaging → Apple App Setup → APNS authentication key to upload your .p8 file.
  11. Register an iOS app in your Firebase project using your bundle identifier. Download GoogleService-Info.plist and add it to your Xcode project's root target.

Create a React Native project

Create a new React Native project and install the required dependencies:

npx react-native@latest init PushTutorial
cd PushTutorial
npm install ably @react-native-firebase/app @react-native-firebase/messaging @notifee/react-native

Configure Android project

Apply the Google Services plugin in android/build.gradle:

// android/build.gradle
buildscript {
    dependencies {
        classpath('com.google.gms:google-services:4.4.2')
    }
}

Then apply it in android/app/build.gradle:

// android/app/build.gradle
apply plugin: 'com.google.gms.google-services'

Also declare the POST_NOTIFICATIONS permission in android/app/src/main/AndroidManifest.xml:

XML

1

2

3

4

5

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    ...
</manifest>

Configure iOS project

Open ios/PushTutorial.xcworkspace in Xcode and add the Push Notifications capability: select your target, go to Signing & Capabilities, and click + Capability.

Add use_modular_headers! to ios/Podfile after prepare_react_native_project!:

prepare_react_native_project!
use_modular_headers!

This is required for Firebase Swift pods (FirebaseCoreInternal, GoogleUtilities) to be integrated as static libraries. Then install the native pods:

cd ios && pod install && cd ..

Then add FirebaseApp.configure() to AppDelegate.swift before React Native starts:

import Firebase

func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
  FirebaseApp.configure()
  // ... rest of existing setup
}

Add all further code to App.tsx.

Step 1: Set up Ably

Replace the contents of App.tsx with the following to initialize the Ably Pub/Sub client, wrap the app in AblyProvider and ChannelProvider, and subscribe to realtime messages on the channel with useChannel:

React

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

import React, {useRef, useState} from 'react';
import {
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import {SafeAreaView} from 'react-native-safe-area-context';
import * as Ably from 'ably';
import {AblyProvider, ChannelProvider, useAbly, useChannel} from 'ably/react';
import messaging from '@react-native-firebase/messaging';
import notifee, {AuthorizationStatus} from '@notifee/react-native';

const CHANNEL_NAME = 'my-first-push-channel';

// Use token authentication in production
const client = new Ably.Realtime({
  key: 'demokey:*****',
  clientId: 'push-tutorial-client',
});

function PushScreen() {
  const [status, setStatus] = useState('Ready to start');
  const [log, setLog] = useState<string[]>([]);

  function addLog(message: string) {
    setLog(prev => [...prev, message]);
  }

  function showStatus(message: string) {
    setStatus(message);
    console.log(message);
  }

  useChannel(CHANNEL_NAME, message => {
    addLog(`Received: ${message.name} - ${JSON.stringify(message.data)}`);
  });

  return (
    <SafeAreaView style={styles.safeArea}>
      <View style={styles.container}>
        <Text style={styles.title}>Ably Push Tutorial</Text>
        <View style={styles.statusBox}>
          <Text style={styles.statusText}>{status}</Text>
        </View>
        <ScrollView style={styles.logBox}>
          {log.map((entry, i) => (
            <Text key={i} style={styles.logEntry}>{entry}</Text>
          ))}
        </ScrollView>
      </View>
    </SafeAreaView>
  );
}

export default function App() {
  return (
    <AblyProvider client={client}>
      <ChannelProvider channelName={CHANNEL_NAME}>
        <PushScreen />
      </ChannelProvider>
    </AblyProvider>
  );
}

const styles = StyleSheet.create({
  safeArea: {flex: 1, backgroundColor: '#fff'},
  container: {flex: 1, padding: 16},
  title: {fontSize: 22, fontWeight: 'bold', textAlign: 'center', marginBottom: 12},
  statusBox: {backgroundColor: '#f0f0f0', padding: 12, borderRadius: 6, marginBottom: 12},
  statusText: {fontSize: 14},
  logBox: {flex: 1, backgroundColor: '#fff', borderWidth: 1, borderColor: '#ddd', borderRadius: 6, padding: 8},
  logEntry: {fontFamily: Platform.OS === 'ios' ? 'Courier' : 'monospace', fontSize: 12, marginBottom: 4},
});
API key:
DEMO ONLY

Key configuration options:

  • key: Your Ably API key.
  • clientId: A unique identifier for this client.

AblyProvider makes the Ably client available to all child components via React context. ChannelProvider scopes child components to my-first-push-channel. useChannel subscribes to realtime messages published on the channel.

Step 2: Set up push notifications

Push notification activation in React Native requires obtaining an FCM registration token and registering the device with Ably. Add the following inside PushScreen:

React

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

// Inside PushScreen:
const client = useAbly();

// Randomly generated on each app start for simplicity — in production, persist this (e.g. using AsyncStorage) to reuse the same device registration across restarts
const [deviceId] = useState(
  `push-tutorial-${Platform.OS}-${Math.random().toString(36).slice(2, 9)}`,
);
const tokenRefreshUnsubscribeRef = useRef<(() => void) | null>(null); // To store the FCM token refresh listener unsubscribe function

async function requestPermission(): Promise<boolean> {
  if (Platform.OS === 'android') {
    // Use notifee for consistent POST_NOTIFICATIONS permission behavior across Android API levels
    const settings = await notifee.requestPermission();
    return settings.authorizationStatus >= AuthorizationStatus.AUTHORIZED;
  }
  // On iOS, request permission using Firebase Messaging which will trigger the native iOS permission dialog
  const authStatus = await messaging().requestPermission();
  return (
    authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    authStatus === messaging.AuthorizationStatus.PROVISIONAL
  );
}

// Save the device registration with Ably using the FCM token
async function saveDeviceRegistration(token: string) {
  await client.push.admin.deviceRegistrations.save({
    id: deviceId,
    clientId: 'push-tutorial-client',
    platform: Platform.OS === 'ios' ? 'ios' : 'android',
    formFactor: 'phone',
    push: {
      recipient: {
        transportType: 'fcm', // FCM on both platforms — messaging().getToken() returns an FCM token even on iOS
        registrationToken: token,
      },
    },
  });
}

async function activatePush() {
  try {
    showStatus('Activating push notifications...');
    const granted = await requestPermission();
    if (!granted) {
      showStatus('Notification permission denied.');
      return;
    }

    await messaging().registerDeviceForRemoteMessages(); // Required to receive push notifications on iOS, no-op on Android
    const fcmToken = await messaging().getToken();
    await saveDeviceRegistration(fcmToken);

    tokenRefreshUnsubscribeRef.current?.();
    tokenRefreshUnsubscribeRef.current = messaging().onTokenRefresh(async newToken => {
      try {
        await saveDeviceRegistration(newToken);
      } catch (error) {
        console.error('Failed to update FCM token:', (error as Ably.ErrorInfo).message);
      }
    });

    showStatus(`Push activated. Device ID: ${deviceId}`);
    addLog(`Push activated. Device ID: ${deviceId}`);
  } catch (error) {
    showStatus(`Failed to activate push: ${(error as Ably.ErrorInfo).message}`);
  }
}

async function deactivatePush() {
  try {
    showStatus('Deactivating push notifications...');
    await client.push.admin.deviceRegistrations.remove(deviceId);
    tokenRefreshUnsubscribeRef.current?.();
    tokenRefreshUnsubscribeRef.current = null;
    showStatus('Push notifications deactivated.');
  } catch (error) {
    showStatus(`Failed to deactivate push: ${(error as Ably.ErrorInfo).message}`);
  }
}

activatePush() does the following:

  1. Requests notification permission from the user.
  2. Obtains the FCM registration token from Firebase.
  3. Registers the device with Ably's push notification service using the token.
  4. Sets up a listener for FCM token refresh events to update the registration with Ably if the token changes.

After successful activation, deviceId can be used to send push notifications to this device.

Handle push notifications

The FCM SDK handles background push notifications automatically and displays them as system notifications. For foreground handling, use @notifee/react-native to display notifications while the app is open.

Add the following inside PushScreen:

React

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

useEffect(() => {
  // Create a default Android notification channel
  if (Platform.OS === 'android') {
    notifee.createChannel({id: 'default', name: 'Default Channel'});
  }

  // Handle foreground push messages
  const unsubscribe = messaging().onMessage(async remoteMessage => {
    const title = remoteMessage.notification?.title ?? 'Push Notification';
    const body = remoteMessage.notification?.body ?? '';
    addLog(`Push received: ${title} — ${body}`);
    await notifee.displayNotification({
      title,
      body,
      android: {channelId: 'default'},
    });
  });

  return () => {
    unsubscribe();
  };
}, []);

Step 3: Subscribe to channel push notifications

To subscribe your device to a channel so it can receive channel-based push notifications, add the following functions inside PushScreen, after the functions from Step 2:

React

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

async function subscribeToChannel() {
  try {
    await client.push.admin.channelSubscriptions.save({
      deviceId,
      channel: CHANNEL_NAME,
    });
    showStatus(`Subscribed to push on channel: ${CHANNEL_NAME}`);
  } catch (error) {
    showStatus(`Failed to subscribe: ${(error as Ably.ErrorInfo).message}`);
  }
}

async function unsubscribeFromChannel() {
  try {
    await client.push.admin.channelSubscriptions.remove({
      deviceId,
      channel: CHANNEL_NAME,
    });
    showStatus(`Unsubscribed from push on channel: ${CHANNEL_NAME}`);
  } catch (error) {
    showStatus(`Failed to unsubscribe: ${(error as Ably.ErrorInfo).message}`);
  }
}

Step 4: Build the UI

Build a UI in your app to add buttons that call all push functions. Update the return statement in PushScreen:

React

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

return (
  <SafeAreaView style={styles.safeArea}>
    <View style={styles.container}>
      <Text style={styles.title}>Ably Push Tutorial</Text>
      <View style={styles.statusBox}>
        <Text style={styles.statusText}>{status}</Text>
      </View>
      <View style={styles.buttons}>
        <TouchableOpacity style={[styles.btn, styles.btnGreen]} onPress={activatePush}>
          <Text style={styles.btnText}>Activate Push</Text>
        </TouchableOpacity>
        <TouchableOpacity style={[styles.btn, styles.btnRed]} onPress={deactivatePush}>
          <Text style={styles.btnText}>Deactivate Push</Text>
        </TouchableOpacity>
        <TouchableOpacity style={[styles.btn, styles.btnPurple]} onPress={subscribeToChannel}>
          <Text style={styles.btnText}>Subscribe to Channel</Text>
        </TouchableOpacity>
        <TouchableOpacity style={[styles.btn, styles.btnOrange]} onPress={unsubscribeFromChannel}>
          <Text style={styles.btnText}>Unsubscribe from Channel</Text>
        </TouchableOpacity>
      </View>
      <ScrollView style={styles.logBox}>
        {log.map((entry, i) => (
          <Text key={i} style={styles.logEntry}>{entry}</Text>
        ))}
      </ScrollView>
      <TouchableOpacity style={[styles.btn, styles.btnGray]} onPress={() => setLog([])}>
        <Text style={styles.btnText}>Clear Log</Text>
      </TouchableOpacity>
    </View>
  </SafeAreaView>
);

Add the button styles to the StyleSheet.create call:

React

1

2

3

4

5

6

7

8

buttons: {gap: 8, marginBottom: 12},
btn: {padding: 14, borderRadius: 6, alignItems: 'center'},
btnText: {color: '#fff', fontWeight: '600'},
btnGreen: {backgroundColor: '#28a745'},
btnRed: {backgroundColor: '#dc3545'},
btnPurple: {backgroundColor: '#6f42c1'},
btnOrange: {backgroundColor: '#fd7e14'},
btnGray: {backgroundColor: '#6c757d', marginTop: 8},

Build and run your app on a physical device:

# Android
npx react-native run-android

# iOS
npx react-native run-ios --device

You can also open each platform project in their respective IDEs and run from there.

Step 5: Publish a push notification

In the app tap Activate Push and wait until the status message displays your device ID.

Publish directly to your device

Publish a push notification directly to your client ID (or device ID using --device-id instead of --client-id) via the Ably CLI:

ably push publish --client-id push-tutorial-client \
  --title "Hello" \
  --body "World!" \
  --data '{"foo":"bar"}'

Publish via a channel

Tap Subscribe to Channel in the app, then publish a push notification to the channel using the Ably CLI:

ably push publish --channel my-first-push-channel \
  --title "Hello" \
  --body "World!" \
  --message '{"name":"greeting","data":"Hello World!"}'

If you tap Unsubscribe from Channel, the device no longer receives push notifications for that channel. Send the same command again and verify that no notification is received.

To see the full list of options for sending push notifications with the Ably CLI, run ably push publish --help or see the Ably CLI push documentation. To send push notifications from your own server code instead of the CLI, see Push notification publishing.

Screenshot of the React Native push tutorial application

Next steps

You can also explore the Ably JavaScript SDK on GitHub, or visit the API references for additional functionality.