@react-native-firebase/appand@react-native-firebase/messaginginstalled (npm install --save @react-native-firebase/app @react-native-firebase/messagingoryarn add @react-native-firebase/app @react-native-firebase/messaging), then rebuild native apps (cd ios && pod installon iOS). From v26, New Architecture is required for packages with a native bridge.- Native Firebase config completed per Getting Started (Android
google-services.json, iOSGoogleService-Info.plist, Expo config plugin, or React Native CLI setup). - On iOS, complete iOS Messaging Setup (Push Notifications, Background Modes, APNs) before expecting delivery.
- A custom XMPP server (for example the
node-xcsnpm package) and a Node.js environment withfirebase-adminif you follow the server samples below. sendMessage()is Android only. Calling it on iOS throwsError('getMessaging().sendMessage() is only supported on Android devices.')in JavaScript before any native call.
This is a reference for using the Firebase Messaging service to send and receive messages directly between devices. Although methods (for example sendMessage()) are provided for sending and receiving messages, additional configuration is needed for direct messaging from devices.
A custom solution is only required if you plan to exchange messages directly between devices (including the message sender device). Configure a solution from this article before expecting device-to-device send/receive to work.
The following describes how to set up a server to handle messages, including:
- A custom XMPP server with XCS for receiving messages.
- Firebase Admin for sending messages.
Flow:
- Send a message from a client device (
sendMessage, Android). - Intercept using a custom XMPP server.
- Push the message to Firebase using
firebase-admin. - Receive the message and acknowledge on other connected devices.
A common instance involves an implementation similar to the following.
import {
getMessaging,
onMessage,
onMessageSent,
onSendError,
sendMessage,
} from '@react-native-firebase/messaging';
const messaging = getMessaging();
onMessage(messaging, message => {
console.log('Received a message', message);
});
onMessageSent(messaging, messageId => {
console.log('Sent a message', messageId);
});
onSendError(messaging, error => {
// `error` is a SendErrorEvent: { messageId, error } (RN-only; no firebase-js-sdk modular equivalent).
console.log('Received an Error', error.messageId, error.error);
});
// Android only. On iOS this throws Error in JavaScript (not a native reject).
sendMessage(messaging, {
data: {
foo: 'bar',
},
});SendErrorEvent is the React Native Firebase type for the onSendError callback payload (messageId plus a native Firebase error).
Although correct for the client API surface, none of the listeners will acknowledge a message or error without a server that bridges FCM and your devices.
Permissions are limited on the client, so an additional solution is required to communicate between FCM and any connected devices.
Deploy a custom server based, for example, on Node XCS.
Below is an example of how to configure a custom XMPP server using node-xcs.
// codeblock-ignore: server-side sample that requires the node-xcs package
const Sender = require('node-xcs').Sender;
async function operation() {
return () => {
console.log('Listening >>>');
// Enter firebase credentials here. {SenderID, ServerKey}
var xcs = new Sender('XXXXXXXX', 'XXXXXXXX');
xcs.start();
xcs.on('message', function (messageId, from, data, category) {
console.log('received message', messageId, from, data, category);
});
xcs.on('receipt', function (messageId, from, data, category) {
console.log('received receipt', arguments);
});
xcs.on('error', e => console.warn('XMPP error.', e));
};
}
async function app() {
await operation();
}
app();For sending messages from the server, use the Firebase Admin SDK.
// codeblock-ignore: server-side sample that requires the firebase-admin package
const admin = require('firebase-admin');
const serviceAccount = require('./service-account.json');
(async () => {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'XXXXXXXX',
});
await admin.messaging().send({
token: 'XXXXXXX',
data: {
foo: 'bar',
},
});
})();
Core / App