I am trying to implement this feature for my app and when the new notification comes and that notification should display on other app’s. but i am getting the notification in background, quite and foreground state. but i want to display them on other app’s in background state and quite state. and i am using this third party library(react-native-floating-bubble) and i am getting the permission for display over the other app’s but i am unable display when notification comes .
for getting the permission i have used this code
you can read about this library react-native-floating bubble
import {crequestPermission} from 'react-native-floating-bubble';
requestPermission()
.then(() => console.log('Permission Granted'))
.catch(() => console.log('Permission is not granted'));
and this code for when the new notification comes that time i want to display over the other app’s
import messaging from '@react-native-firebase/messaging';
import notifee, {AndroidImportance} from '@notifee/react-native';
import {Alert, DeviceEventEmitter} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {showFloatingBubble} from 'react-native-floating-bubble';
import Sound from 'react-native-sound'; // Import Sound for playing audio
export 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);
}
}
// Function to handle showing floating bubble and playing sound
const handleNewNotification = async () => {
try {
// Show floating bubble
showFloatingBubble(10, 10).then(() => console.log('Floating Bubble Added'));
// Play notification sound (adjust the sound file path accordingly)
const sound = new Sound('notification.mp3', Sound.MAIN_BUNDLE, error => {
if (error) {
console.log('Error loading sound:', error);
return;
}
// Play the sound when it's loaded
sound.play(success => {
if (success) {
console.log('Sound played successfully');
} else {
console.log('Sound playback failed');
}
});
});
} catch (error) {
console.error('Error handling new notification:', error);
}
};
export const notificationListener = () => {
messaging().onNotificationOpenedApp(async remoteMessage => {
handleNewNotification();
console.log(
'Notification caused app to open from background state:',
remoteMessage.notification,
);
DeviceEventEmitter.emit('notificationListener', remoteMessage);
console.log('Basya, Laxman');
// Show floating bubble and play sound on notification open
});
// Check whether an initial notification is available
messaging()
.getInitialNotification()
.then(async remoteMessage => {
if (remoteMessage) {
await AsyncStorage.setItem(
'notificationListener',
JSON.stringify(remoteMessage),
);
console.log(
'Notification caused app to open from quit state:',
remoteMessage,
);
// Show floating bubble and play sound on initial notification
handleNewNotification();
}
});
};
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background:', remoteMessage);
// Handle the new notification
handleNewNotification(remoteMessage.data);
});
export const getToken = async () => {
await messaging().registerDeviceForRemoteMessages();
const token = await messaging().getToken();
console.log('================Token==========');
console.log(token);
console.log('================Token==========');
};
export async function onDisplayNotification() {
try {
await notifee.requestPermission();
const channelId = await notifee.createChannel({
id: 'notification_sound',
name: 'service_app',
sound: {uri: 'notification'},
importance: AndroidImportance.HIGH,
});
const acceptAction = {title: 'Accept', pressAction: {id: 'accept_action'}};
const rejectAction = {title: 'Reject', pressAction: {id: 'reject_action'}};
await notifee.displayNotification({
title: 'Yellowsense',
body: 'You Got New Customer',
android: {
channelId,
pressAction: {id: 'default'},
actions: [acceptAction, rejectAction],
smallIcon: 'ic_logo',
},
});
notifee.onForegroundEvent(async ({type, detail}) => {
if (type === 'press') {
const {pressAction} = detail;
if (pressAction) {
switch (pressAction.id) {
case 'accept_action':
console.log('Accept action pressed');
break;
case 'reject_action':
console.log('Reject action pressed');
break;
}
}
}
});
} catch (error) {
console.error('Error displaying notification:', error);
}
}
so please anyone who know how to do this can you please help to solve this problem