messaging().onNotificationOpenedApp is never triggered, messaging().getInitialNotification() is triggered but remoteMessage is always null [expo]

  • setBackgroundMessageHandler is triggered on background state.
  • onMessage is triggered on foreground state.
  • onNotificationOpenedApp is never triggered.
  • getInitialNotification is always returning null.

please help me find the solution for the above. I have tried many different solutions available but none of them worked.

I am using expo prebuild and expo sdk50 with expo router.

I have following modules installed:
“@react-native-firebase/app”: “^19.2.2”,
“@react-native-firebase/messaging”: “^19.2.2”,
“expo”: “^50.0.15”,
“react-native”: “0.73.6”,

Below is my _layout.tsx file which is the entry point for my app.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// imports here
export {
// Catch any errors thrown by the Layout component.
ErrorBoundary,
} from 'expo-router';
export const unstable_settings = {
// Ensure that reloading on `/modal` keeps a back button present.
initialRouteName: '(tabs)',
};
// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();
//Message Handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});
export default function RootLayout() {
const [loaded, error] = useFonts({
outfitLight: require('../assets/fonts/Outfit-Light.ttf'),
outfit: require('../assets/fonts/Outfit-Regular.ttf'),
outfitMedium: require('../assets/fonts/Outfit-Medium.ttf'),
outfitBold: require('../assets/fonts/Outfit-Bold.ttf'),
outfitSemiBold: require('../assets/fonts/Outfit-SemiBold.ttf'),
...FontAwesome.font,
});
const router = useRouter();
// Expo Router uses Error Boundaries to catch errors in the navigation tree.
useEffect(() => {
if (error) throw error;
}, [error]);
useEffect(() => {
if (loaded) {
SplashScreen.hideAsync();
}
}, [loaded]);
if (!loaded) {
return null;
}
NativeWindStyleSheet.setOutput({
default: "native",
});
return (
<ApolloProvider client={client}>
<RootLayoutNav />
</ApolloProvider>
);
}
</code>
<code>// imports here export { // Catch any errors thrown by the Layout component. ErrorBoundary, } from 'expo-router'; export const unstable_settings = { // Ensure that reloading on `/modal` keeps a back button present. initialRouteName: '(tabs)', }; // Prevent the splash screen from auto-hiding before asset loading is complete. SplashScreen.preventAutoHideAsync(); //Message Handler messaging().setBackgroundMessageHandler(async remoteMessage => { console.log('Message handled in the background!', remoteMessage); }); export default function RootLayout() { const [loaded, error] = useFonts({ outfitLight: require('../assets/fonts/Outfit-Light.ttf'), outfit: require('../assets/fonts/Outfit-Regular.ttf'), outfitMedium: require('../assets/fonts/Outfit-Medium.ttf'), outfitBold: require('../assets/fonts/Outfit-Bold.ttf'), outfitSemiBold: require('../assets/fonts/Outfit-SemiBold.ttf'), ...FontAwesome.font, }); const router = useRouter(); // Expo Router uses Error Boundaries to catch errors in the navigation tree. useEffect(() => { if (error) throw error; }, [error]); useEffect(() => { if (loaded) { SplashScreen.hideAsync(); } }, [loaded]); if (!loaded) { return null; } NativeWindStyleSheet.setOutput({ default: "native", }); return ( <ApolloProvider client={client}> <RootLayoutNav /> </ApolloProvider> ); } </code>
// imports here

export {
  // Catch any errors thrown by the Layout component.
  ErrorBoundary,
} from 'expo-router';

export const unstable_settings = {
  // Ensure that reloading on `/modal` keeps a back button present.
  initialRouteName: '(tabs)',
};


// Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync();

//Message Handler
messaging().setBackgroundMessageHandler(async remoteMessage => {
  console.log('Message handled in the background!', remoteMessage);
}); 

export default function RootLayout() {
  const [loaded, error] = useFonts({
    outfitLight: require('../assets/fonts/Outfit-Light.ttf'),
    outfit: require('../assets/fonts/Outfit-Regular.ttf'),
    outfitMedium: require('../assets/fonts/Outfit-Medium.ttf'),
    outfitBold: require('../assets/fonts/Outfit-Bold.ttf'),
    outfitSemiBold: require('../assets/fonts/Outfit-SemiBold.ttf'),
    ...FontAwesome.font,
  });
  
  const router = useRouter();

  // Expo Router uses Error Boundaries to catch errors in the navigation tree.
  useEffect(() => {
    if (error) throw error;
  }, [error]);

  useEffect(() => {
    if (loaded) {
      SplashScreen.hideAsync();
    }
  }, [loaded]);

  if (!loaded) {
    return null;
  }

  
  NativeWindStyleSheet.setOutput({
    default: "native",
  });

  return (
      <ApolloProvider client={client}>
            <RootLayoutNav />
      </ApolloProvider>
    
    );
}

I have this in my index.tsx file which is the first page of my app

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>useEffect(() => {
(async () => await messaging().registerDeviceForRemoteMessages())();
const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log("FCM Foregorund message Handled");
Alert.alert("FCM Foregorund message Handled");
});
messaging().onNotificationOpenedApp(async (remoteMessage) => {
// The below code gets never executed
Alert.alert('here');
console.log(
'Notification caused app to open from background state:',
remoteMessage,
);
});
messaging()
.getInitialNotification()
.then(async (remoteMessage) => {
console.log("s: : : : : :",remoteMessage); // always prints null
if (remoteMessage) {
// Never reached
Alert.alert('here');
console.log(
'Notification caused app to open from quit state:',
remoteMessage,
);
}
});
return unsubscribe;
}, []);
</code>
<code>useEffect(() => { (async () => await messaging().registerDeviceForRemoteMessages())(); const unsubscribe = messaging().onMessage(async remoteMessage => { console.log("FCM Foregorund message Handled"); Alert.alert("FCM Foregorund message Handled"); }); messaging().onNotificationOpenedApp(async (remoteMessage) => { // The below code gets never executed Alert.alert('here'); console.log( 'Notification caused app to open from background state:', remoteMessage, ); }); messaging() .getInitialNotification() .then(async (remoteMessage) => { console.log("s: : : : : :",remoteMessage); // always prints null if (remoteMessage) { // Never reached Alert.alert('here'); console.log( 'Notification caused app to open from quit state:', remoteMessage, ); } }); return unsubscribe; }, []); </code>
useEffect(() => {
    (async () => await messaging().registerDeviceForRemoteMessages())();

    const unsubscribe = messaging().onMessage(async remoteMessage => {
      console.log("FCM Foregorund message Handled");
      Alert.alert("FCM Foregorund message Handled");
    });

    messaging().onNotificationOpenedApp(async (remoteMessage) => {
      // The below code gets never executed
      Alert.alert('here');
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage,
      );
    });

    messaging()
      .getInitialNotification()
      .then(async (remoteMessage) => {
        console.log("s: : : : : :",remoteMessage); // always prints null
        if (remoteMessage) {
          // Never reached
          Alert.alert('here');
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage,
          );
        }
      });

    return unsubscribe;
  }, []);

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