What is @react-native-firebase/messaging?
@react-native-firebase/messaging is a React Native module that provides Firebase Cloud Messaging (FCM) integration for sending and receiving push notifications. It allows developers to handle background and foreground notifications, manage device tokens, and customize notification behavior.
What are @react-native-firebase/messaging's main functionalities?
Receiving Messages
This feature allows the app to receive messages when it is in the foreground. The `onMessage` listener is triggered whenever a new FCM message arrives.
import messaging from '@react-native-firebase/messaging';
messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));
});
Requesting Permission
This feature requests the user's permission to receive push notifications. It checks the authorization status and logs it.
import messaging from '@react-native-firebase/messaging';
async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
}
}
Getting the FCM Token
This feature retrieves the FCM token for the device, which is necessary for sending targeted notifications to that device.
import messaging from '@react-native-firebase/messaging';
async function getToken() {
const token = await messaging().getToken();
console.log('FCM Token:', token);
}
Handling Background Messages
This feature allows the app to handle messages when it is in the background. The `setBackgroundMessageHandler` is triggered for background messages.
import messaging from '@react-native-firebase/messaging';
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});
Other packages similar to @react-native-firebase/messaging
react-native-push-notification
react-native-push-notification is a popular package for handling local and remote notifications in React Native apps. It provides a wide range of features for scheduling, displaying, and managing notifications. Compared to @react-native-firebase/messaging, it offers more flexibility for local notifications but lacks the deep integration with Firebase services.
notifee
notifee is a powerful notification library for React Native that provides advanced features for creating and managing notifications. It offers more customization options and better support for notification channels compared to @react-native-firebase/messaging. However, it does not provide direct integration with Firebase Cloud Messaging.
react-native-firebase
react-native-firebase is a comprehensive package that includes various Firebase services, including messaging, authentication, and analytics. While it provides similar messaging functionalities as @react-native-firebase/messaging, it is a larger package that includes many other Firebase services, making it suitable for apps that require multiple Firebase features.
React Native Firebase - Messaging
React Native Firebase provides native integration of Firebase Cloud Messaging (FCM) for both Android & iOS. FCM is a
cost free service, allowing for server-device and device-device communication.
The React Native Firebase Messaging module provides a simple JavaScript API to
interact with FCM.
> Learn More
Installation
Requires @react-native-firebase/app
to be installed.
yarn add @react-native-firebase/messaging
Documentation
Additional Topics
License
Built and maintained with 💛 by Invertase.