//this is my notificationservice.tsx file
`import messaging from '@react-native-firebase/messaging';
import AsyncStorage from '@react-native-async-storage/async-storage';
import axios from 'axios';
// Function to request user permission for notifications
export async function requestUserPermission() {
try {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
await getFcmToken();
} else {
console.log('Notification permission denied.');
}
} catch (error) {
console.error('Error requesting permission:', error);
}
}
// Function to get the Firebase Cloud Messaging (FCM) token
const getFcmToken = async () => {
try {
let fcmToken = await AsyncStorage.getItem('fcmToken');
console.log('Stored FCM Token:', fcmToken);
if (!fcmToken) {
const newFcmToken = await messaging().getToken();
if (newFcmToken) {
console.log('New FCM Token generated:', newFcmToken);
await AsyncStorage.setItem('fcmToken', newFcmToken);
}
}
} catch (error) {
console.error('Error getting or setting FCM Token:', error);
}
}
// Function to listen for incoming notifications
export const notificationListener = async () => {
// Listener for when the app is in the background and the user opens the notification
messaging().onNotificationOpenedApp(remoteMessage => {
console.log('Notification opened from background state:', remoteMessage.notification);
});
// Listener for receiving notifications while the app is in the foreground
messaging().onMessage(remoteMessage => {
console.log('Received notification in foreground state:', remoteMessage);
});
// Check if the app was opened from a notification (when it was previously quit)
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log('Notification caused app to open from quit state:', remoteMessage.notification);
} else {
console.log('No initial notification found.');
}
})
.catch(error => {
console.error('Error getting initial notification:', error);
});
}
with this code i found issue that when my phone is locked or when my application is closed when i send notification with FCM tester the notification shown in my screen but when i try to send notification ,when my application is active i cant able to show notification on top of the my screen.
I also add all dependencies in build.gradle file and Androidmanifest.xml file
[and this is my index.js file for set background notification. this background notification is also work ] (https://i.stack.imgur.com/rvmXl.png)
[look this image in this image](https://i.stack.imgur.com/mRi0d.png) only onmessage working . with getinitial notification i found No initial notification found, null this i want that all this message state is work.