React native – implemented Firebase Cloud Messaging & Notifee.
I’m trying to cover all the possible notification-scenarios but having issues with some of the cases.
I do receive the notifications to my physical devices (both platforms) and most of the logics work as should.
Here is my current code:
import { PermissionsAndroid } from 'react-native';
import messaging, { FirebaseMessagingTypes } from '@react-native-firebase/messaging';
import notifee, { AndroidImportance, EventType } from '@notifee/react-native';
import { logger } from '../managers/loggingManager';
import { markNotificationAsRead } from '../apiControllers/authApi';
import { androidVersion, isIOS } from './platformUtil';
// MARK: - Create Listeners
export const initNotificationHandling = async () => {
/** App on FOREGROUND --> display notification */
messaging().onMessage(async remoteMessage => {
displayForegroundNotification(remoteMessage);
});
/** App on FOREGROUND --> user tapped the notification
* or
* App on QUIT in *iOS* --> user tapped the notification to open app */
notifee.onForegroundEvent(({ type, detail }) => {
if (type === EventType.PRESS) {
logger.object('FOREGROUND Notification PRESSED', detail.notification);
}
});
/** App opened from BACKGROUND in *Android* */
messaging().onNotificationOpenedApp(remoteMessage => {
logger.object('ANDROID was opened from BACKGROUND', remoteMessage);
});
// vvvvvvv NOT WORKING BLOCK vvvvvvv
const initialNotification = await notifee.getInitialNotification();
if (initialNotification) {
logger.object('Notification caused application to open', initialNotification.notification);
logger.object('Press action used to open the app', initialNotification.pressAction);
}
/** App on QUIT --> user taps "mark as read" on notification */
notifee.onBackgroundEvent(async ({ type, detail }) => {
const { notification, pressAction } = detail;
if (!notification || !pressAction) return;
if (type === EventType.ACTION_PRESS && pressAction.id === 'mark-as-read' && notification.id) {
await markNotificationAsRead(notification.id);
await notifee.cancelNotification(notification.id);
}
});
};
// MARK: - Display Notification
const displayForegroundNotification = async (
remoteMessage: FirebaseMessagingTypes.RemoteMessage
) => {
logger.object('FOREGROUND Notification received', remoteMessage.notification);
// mandatory for Android
const channelId = await notifee.createChannel({
id: 'General',
name: 'General Notifications',
importance: AndroidImportance.HIGH,
});
try {
await notifee.displayNotification({
title: remoteMessage.notification?.title,
body: remoteMessage.notification?.body,
android: { channelId, importance: AndroidImportance.HIGH },
});
} catch (error) {
logger.error(`Error displaying FOREGROUND Notification: ${error}`);
}
};
The entire initNotificationHandling
function is called first thing on my appInit
logic.
What I still can’t figure out –
- App was QUIT in Android and opened from tapping notification – how do i access the notification? My current code doesn’t work – the
await notifee.getInitialNotification()
always returns null. - What do i need
messaging().setBackgroundMessageHandler()
for? From my understanding its functionality is already covered in my other code. - How do I make my notification have the option to marked as read? So I can test if the network request is actually being sent and that code is indeed working.
I assume I did implement FCM correctly as I am getting the FCM token and am receiving notification to my devices via Postman. I did request permissions in both platforms as documented.
I’ve read the entire Firebase Cloud Messaging documentation as well as Notifee’s documentation.
Tried a bunch of online guides on Medium.
Also ChatGPT wasn’t helpful.
NoamKu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.