Skip to main content

Expo

This guide discusses how to set up your Expo app to get push notifications from Stream for the non-ringing calls that your user will receive.

Add push provider credentials to Stream

Please follow the below guides for adding appropriate push providers to Stream:

Install Dependencies

Terminal
npx expo install expo-notifications
npx expo install expo-task-manager
npx expo install @notifee/react-native

So what did we install precisely?

  • expo-notifications and expo-task-manager for handling incoming Firebase Cloud Messaging notifications on Android and iOS.

Add Firebase credentials

  1. To create a Firebase project, go to the Firebase console and click on Add project.

  2. In the console, click the setting icon next to Project overview and open Project settings. Then, under Your apps, click the Android icon to open Add Firebase to your Android app and follow the steps. Make sure that the Android package name you enter is the same as the value of android.package from your app.json.

  3. After registering the app, download the google-services.json file and place it in your project's root directory.

  4. In app.json, add an android.googleServicesFile field with the relative path to the downloaded google-services.json file. If you placed it in the root directory, the path is:

app.json
{
"android": {
"googleServicesFile": "./google-services.json"
}
}
note

The google-services.json file contains unique and non-secret identifiers of your Firebase project. For more information, see Understand Firebase Projects.

Add the config plugin property

In app.json, in the plugins field, add true to the enableNonRingingPushNotifications property in the @stream-io/video-react-native-sdk plugin.

app.json
{
"plugins": [
[
"@stream-io/video-react-native-sdk",
{
"enableNonRingingPushNotifications": true
}
],
// your other plugins
]
}

Setup the push config for the SDK

The SDK automatically processes the non ringing call push notifications once the setup above is done if the push config has been set using StreamVideoRN.setPushConfig. To do this follow the steps below,

Add the ability to statically navigate to screens in your app

When a user taps on the push notification and the JS engine is not ready, they should still be able to navigate to the screen that shows the active call. You can achieve this by using imperative navigation in the expo router.

The following is an example implementation of a utility file that has helpers to statically navigate in the app:

src/utils/staticNavigation.ts
import { User } from '@stream-io/video-react-native-sdk';
import { router } from 'expo-router';

/**
* This is used to run the navigation logic from root level
*/
export const staticNavigateToActiveCall = () => {
const intervalId = setInterval(async () => {
// add any requirements here (like authentication)
if (GlobalState.hasAuthentication) {
clearInterval(intervalId);
router.push('/activecall');
}
}, 300);
};

export const staticNavigateToLivestreamCall = () => {
const intervalId = setInterval(async () => {
// add any requirements here (like authentication)
if (GlobalState.hasAuthentication) {
clearInterval(intervalId);
router.push('/livestream');
}
}, 300);
};

Setup the push config

Once we have set up the methods to navigate the app from a static method we are ready to call the StreamVideoRN.setPushConfig method. Below is an example of how this method can be called,

src/utils/setPushConfig.ts
import {
StreamVideoClient,
StreamVideoRN,
} from '@stream-io/video-react-native-sdk';
import { AndroidImportance } from '@notifee/react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { STREAM_API_KEY } from '../../constants';
import { staticNavigateToRingingCall, staticNavigateToLivestreamCall } from './staticNavigationUtils';

export function setPushConfig() {
StreamVideoRN.setPushConfig({
// pass true to inform the SDK that this is an expo app
isExpo: true,
ios: {
// add your push_provider_name for iOS that you have setup in Stream dashboard
pushProviderName: __DEV__ ? 'apn-video-staging' : 'apn-video-production',
},
android: {
// add your push_provider_name for Android that you have setup in Stream dashboard
pushProviderName: __DEV__
? 'firebase-video-staging'
: 'firebase-video-production',
// configure the notification channel to be used for non ringing calls for Android.
callChannel: {
id: 'stream_call_notifications',
name: 'Call notifications',
// This importance will ensure that the notification will appear on-top-of applications.
importance: AndroidImportance.HIGH,
sound: "default",
},
// configure the functions to create the texts shown in the notification
// for non ringing calls in Android.
callNotificationTextGetters: {
getTitle(type, createdUserName) {
if (type === 'call.live_started') {
return `Call went live, it was started by ${createdUserName}`;
} else {
return `${createdUserName} is notifying you about a call`;
}
},
getBody(_type, createdUserName) {
return 'Tap to open the call';
},
},
},
// optional: add the callback to be executed when a non ringing call notification is tapped
onTapNonRingingCallNotification: () => {
const [callType, callId] = call_cid.split(':');
if (callType === 'livestream') {
staticNavigateToLivestreamCall();
} else {
staticNavigateToActiveCall();
}
},
// add the async callback to create a video client
// for incoming calls in the background on a push notification
createStreamVideoClient: async () => {
// note that since the method is async,
// you can call your server to get the user data or token or retrieve from offline storage.
const userId = await AsyncStorage.getItem('@userId');
const userName = await AsyncStorage.getItem('@userName');
// an example promise to fetch token from your server
const tokenProvider = () => yourServer.getTokenForUser(userId).then((auth) => auth.token);
const user = { id: userId, name: userName };
return StreamVideoClient.getOrCreateInstance({
apiKey: STREAM_API_KEY, // pass your stream api key
user,
tokenProvider,
});
},
});
}

Now, call the method outside of your application cycle. That is inside index.js. This is because the app can be opened from a dead state through a push notification and in that case, we need to use the config as soon as the JS bridge is initialized. Following is an example,

index.js
import 'expo-router/entry';
import { setPushConfig } from './utils/setPushConfig';

setPushConfig();

Request for notification permissions

At an appropriate place in your app, request for notification permissions from the user. Below is a small example of how to request permissions in Expo:

import * as Notifications from 'expo-notifications';
await Notifications.requestPermissionsAsync();

Disabling push - usually on logout

In some cases you would want to disable push from happening. For example, if user logs out of your app. Or if the user switches. You can disable push like below:

import { StreamVideoRN } from '@stream-io/video-react-native-sdk';

await StreamVideoRN.onPushLogout();

Troubleshooting

  • During development, you may be facing a situation where push notification is shown but its events like accepting or rejecting a call don't work. This is because, during hot module reloading the global event listeners may get de-registered. To properly test during development, make sure that you fully restart the app or test in release mode without the metro packager.
  • You can check the "Webhook & Push Logs" section in the Stream Dashboard to see if Notifications were sent by Stream.
  • If you are still having trouble with Push Notifications, please submit a ticket to us at support.

Did you find this page helpful?