Handling Push Notification in React Native with FCM and Notifee

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 –

  1. 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.
  2. What do i need messaging().setBackgroundMessageHandler() for? From my understanding its functionality is already covered in my other code.
  3. 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.

New contributor

NoamKu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật