npm install @react-native-firebase/app @react-native-firebase/messaging
Update your android/app/build.gradle. In dependencies add below
implementation 'com.google.firebase:firebase-messaging'
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 granted');
} else {
console.log('Authorization declined');
}
}
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('Message handled in the background:', remoteMessage);
});
export default requestUserPermission;
import messaging from '@react-native-firebase/messaging';
async function registerForegroundListener() {
messaging().onMessage((message) => {
// Process the message data and display any notifications or actions
console.log('Got foreground message:', message);
});
}
// Call the listener somewhere in your app's initialization (e.g., main component)
registerForegroundListener();
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
// Process the message data and schedule or display notifications
console.log('Message handled in the background:', remoteMessage);
});