expo / expo

An open-source framework for making universal native apps with React. Expo runs on Android, iOS, and the web.
https://docs.expo.dev
MIT License
32.75k stars 5.21k forks source link

Fcm broken in quit state #29757

Open Protonosgit opened 3 months ago

Protonosgit commented 3 months ago

Minimal reproducible example

https://github.com/Protonosgit/StickerSmash

Which package manager are you using? (Yarn is recommended)

npm

If the issue is web-related, please select the bundler (web.bundler in the app.json)

metro

Summary

When sending notifications they only arrive in foreground and background state but never in quit state. Appending a title/body to the custom notification (notifee) allows the notification to show up in quit state as well but the handler still fails. The setBackgroundMessageHandler handler itself works and triggers when the app is in background but not in a closed state. The notification is received by the app which starts in headless mode but then fails. (see bellow)

Notification test in all states:

Screenshot 2024-05-30 165046 Context: 1. Received in foreground 2. Received in background 3. Received in quit state I'm using expo-router and everything was implemented as suggested in the docs.

Also interesting:

Other people had this issue With expo file based routing enabled so I suppose this could be a problem with the startup sequence of the app ? Production builds crash on startup when the handler is implemented but prebuilds work fine?

Environment

expo-env-info 1.2.0 environment info: System: OS: Windows 11 10.0.22631 Binaries: Node: 22.0.0 - C:\Program Files\nodejs\node.EXE npm: 10.8.0 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: AI-233.14808.21.2331.11709847 npmPackages: expo: ^51.0.8 => 51.0.12 expo-router: ~3.5.14 => 3.5.16 react: 18.2.0 => 18.2.0 react-native: ^0.74.2 => 0.74.2 Expo Workflow: bare

l3815 commented 2 months ago

same

pawel565 commented 2 months ago

same

MartinHarkins commented 1 month ago

Edit: I do think this involves the expo-router team.

Got the background messages going in kill state, though I would appreciate feedback on this solution by the expo-router team:

// src/index.ts

import 'expo-router/entry'

import messaging from '@react-native-firebase/messaging'

// must register the handler as soon as possible.
messaging().setBackgroundMessageHandler(async (m) => {
  console.log('GOT FROM FIREBASE', JSON.stringify(m))
})

and in package.json:

- "main": "expo-router/entry",
+ "main": "src/index.ts",

The setup we have in order to close in on traditional setups is a RootLayout:

// src/nav/_layout.tsx

import { Slot } from 'expo-router'
import App from '../App'

export default function RootLayout() {
  return (
    <App>
      <Slot />
    </App>
  )

And therefore an App with usually all the setup

// src/App.tsx

// bunch of setup

function App({ children }: PropsWithChildren) {
  return (
    <WhateverProviders>
      {children}
    </WhateverProviders
  )
}

Though adding the messaging().setBackgroundMessageHandler(...) within App.tsx was not "early" enough and therefore found this src/index.ts workaround...

Protonosgit commented 1 month ago

I already suspected something similar here but wasn't really sure so I tried to collect similar reports. Thank you for your contribution I will try your approach!