Expo

The React Native Expo SDK makes it easy to integrate TalkJS into your Expo app. This guide shows you how to set up and receive push notifications from TalkJS on Expo for both iOS and Android.

The guide assumes that you have set up the TalkJS Expo SDK as shown in the Getting Started guide.

Android

Begin by configuring Firebase on the TalkJS dashboard. See: Configure Firebase Cloud Messaging.

Configure Firebase plugins

If you haven't already, make sure to add your project on the Firebase console and download the resultant google-services.json to your project's root folder. You will need to provide the path to the google-services.json file in your project's app.json as shown:

1{
2 "expo": {
3 "android": {
4 "googleServicesFile": "./google-services.json",
5 "package": "my_package_name"
6 }
7 }
8}

Replace my_package_name above with your application ID.

Having followed the Install TalkJS step in the Getting Started guide, you should have @react-native-firebase/app and @react-native-firebase/messaging installed. Add both to the plugins array of your app.json file, as shown:

1{
2 "expo": {
3 "plugins": [
4 "@react-native-firebase/app"
5 "@react-native-firebase/messaging"
6 ]
7 }
8}

Configure Notifee

The Notifee package requires its native modules to be defined under extraMavenRepos in your project's app.json. First, install expo-build-properties as shown:

1npx expo install expo-build-properties

Then, update your app.json as shown:

1{
2 "expo": {
3 "plugins": [
4 // Other plugins
5 [
6 "expo-build-properties",
7 {
8 "android": {
9 "extraMavenRepos": [
10 "../../node_modules/@notifee/react-native/android/libs"
11 ]
12 }
13 }
14 ]
15 ]
16 }
17}

Request push notification permissions (Android 13 and higher)

If your app is targeting Android 13 or higher, you are required to request for a new runtime permission called POST_NOTIFICATIONS in order for the app to display notifications.

To request a new runtime permission, first declare the permission in your project's app.json, as shown:

1{
2 "expo": {
3 "android": {
4 "permissions": ["android.permission.POST_NOTIFICATIONS"]
5 }
6 }
7}

Then request the permission at runtime using PermissionsAndroid from the react-native package, as follows:

1import { Platform, PermissionsAndroid } from 'react-native';
2
3// Other code...
4
5if (Platform.OS === 'android') {
6 PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);
7}

You can request for this permission at any point in your app's flow that makes sense for your project and its users.

iOS

The TalkJS Expo SDK currently ONLY supports sending push notifications to iOS via Firebase. Before continuing, make sure to configure Firebase on the TalkJS dashboard. See: Configure Firebase Cloud Messaging.

Configure Firebase Plugins

If you haven't already, make sure to add your project on the Firebase console. Also, don't forget to upload your APNs authentication key to your project's Firebase console.

After adding your project to the Firebase console, download the resultant GoogleService-Info.plist to your project's root folder. Provide the path to the GoogleService-Info.plist file in your project's app.json, as shown:

1{
2 "expo": {
3 "ios": {
4 "googleServicesFile": "./GoogleService-Info.plist",
5 "bundleIdentifier": "my_package_name"
6 }
7 }
8}

Replace my_package_name above with your app's Bundle ID.

Having followed the Install TalkJS step in the Getting Started guide, you should have @react-native-firebase/app and @react-native-firebase/messaging installed. Add them to the plugins array of your app.json file as shown:

1{
2 "expo": {
3 "plugins": [
4 "@react-native-firebase/app"
5 "@react-native-firebase/messaging"
6 ]
7 }
8}

The Firebase iOS SDKs require use_frameworks as part of the build process. To help with this, install expo-build-properties as shown:

1npx expo install expo-build-properties

Then add "useFrameworks": "static" to your app.json as shown:

1{
2 "expo": {
3 "plugins": [
4 // Other plugins
5 [
6 "expo-build-properties",
7 {
8 "ios": {
9 "useFrameworks": "static"
10 }
11 }
12 ]
13 ]
14 }
15}

Request permissions

Apple requires that an iOS app requests for the necessary notification permissions before being able to display push notifications. You can request for these permissions at any point in your app's flow by using the @react-native-firebase/messaging library, as shown:

1import { Platform } from 'react-native';
2import Messaging from '@react-native-firebase/messaging';
3
4// Other code...
5
6if (Platform.OS === 'ios') {
7 Messaging().requestPermission();
8}

You can then proceed to register the push notification handlers.

Register push notification handlers

To register push notification handlers, make a call to registerPushNotificationHandlers inside your app's App.js/App.tsx file.

Avoid calling the registerPushNotificationHandlers function inside any component, because when your app is stopped (whether by the user swiping away from the recents screen, or due to low memory on the device) notifications received won't start the full app. This means that no component code runs and therefore no notifications are displayed.

Aside from setting up the notification handlers, the registerPushNotificationHandlers function also creates an Android notification channel. Without a notification channel, notifications won't appear on Android devices.

Below is an example App.js file:

1import { StatusBar } from 'expo-status-bar';
2import { StyleSheet, Text, View, PermissionsAndroid, Platform } from 'react-native';
3import { registerPushNotificationHandlers } from '@talkjs/expo';
4import Messaging from '@react-native-firebase/messaging';
5
6if (Platform.OS === 'android') {
7 PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);
8} else if (Platform.OS === 'ios') {
9 Messaging().requestPermission();
10}
11
12registerPushNotificationHandlers(
13 {
14 channelId: 'com.anonymous.TalkjsLocalExpo',
15 channelName: 'Messages',
16 badge: true,
17 }
18);
19
20export default function App() {
21 return (
22 <View style={styles.container}>
23 <Text>Open up App.js to start working on your app!</Text>
24 <StatusBar style="auto" />
25 </View>
26 );
27}
28
29const styles = StyleSheet.create({
30 container: {
31 flex: 1,
32 backgroundColor: '#fff',
33 alignItems: 'center',
34 justifyContent: 'center',
35 },
36});

Also here's an example app.json file showing configuration of Firebase for both Android and iOS:

1{
2 "expo": {
3 "name": "TalkjsLocalExpo",
4 "slug": "TalkjsLocalExpo",
5 "version": "1.0.0",
6 "orientation": "portrait",
7 "icon": "./assets/icon.png",
8 "userInterfaceStyle": "light",
9 "splash": {
10 "image": "./assets/splash.png",
11 "resizeMode": "contain",
12 "backgroundColor": "#ffffff"
13 },
14 "assetBundlePatterns": [
15 "**/*"
16 ],
17 "ios": {
18 "supportsTablet": true,
19 "googleServicesFile": "./GoogleService-Info.plist",
20 "bundleIdentifier": "com.anonymous.TalkjsLocalExpo"
21 },
22 "android": {
23 "adaptiveIcon": {
24 "foregroundImage": "./assets/adaptive-icon.png",
25 "backgroundColor": "#ffffff"
26 },
27 "permissions": ["android.permission.POST_NOTIFICATIONS"],
28 "googleServicesFile": "./google-services.json",
29 "package": "com.anonymous.TalkjsLocalExpo"
30 },
31 "web": {
32 "favicon": "./assets/favicon.png"
33 },
34 "plugins": [
35 [
36 "expo-build-properties",
37 {
38 "android": {
39 "extraMavenRepos": [
40 "../../node_modules/@notifee/react-native/android/libs"
41 ]
42 },
43 "ios": {
44 "useFrameworks": "static"
45 }
46 }
47 ],
48 "@react-native-firebase/app",
49 "@react-native-firebase/messaging"
50 ]
51 }
52}