[Android] v6 messaging().setBackgroundMessageHandler not working if app gets killed

Issue

“react”: “16.9.0”,
“react-native”: “0.61.5”
“@react-native-firebase/app”: “^6.3.4”,
“@react-native-firebase/messaging”: “^6.3.4”,

In Android if the app gets killed messaging().setBackgroundMessageHandler is not invoked.
So as of v6 notifications functionality is not available anymore I was planning to use react-native-firebase (only the messaging module) to get silent pushes and then using other lib to trigger a local notification to mimic react-native-firebase previous versions behaviour.
Btw I according to the doc messaging().setBackgroundMessageHandler is only available for Android.
How can I use react-native-firebase for getting notifications on iOS when the app is closed.

Thanks in advance.

Federico


Project Files

Javascript

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

export const firebasePushSetup = async () => {
  await messaging().registerForRemoteNotifications();

  const token = await messaging().getToken();
  console.log('TOKEN =', token);

  const granted = await messaging().requestPermission();
  console.log('GRANTED =', granted);

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

  const unsubscribe = messaging().onMessage(async remoteMessage => {
    console.log('FCM Message Data:', remoteMessage.data);
  });

  return unsubscribe;
};

Click To Expand

package.json:

# N/A

firebase.json for react-native-firebase v6:

# N/A

iOS

Click To Expand

ios/Podfile:

  • I’m not using Pods
  • I’m using Pods and my Podfile looks like:
# N/A

AppDelegate.m:

// N/A

Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->

Environment

Click To Expand

react-native info output:

 OUTPUT GOES HERE
  • Platform that you’re experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you’re using that has this issue:
    • e.g. 5.4.3
  • Firebase module(s) you’re using that has the issue:
    • e.g. Instance ID
  • Are you using TypeScript?
    • Y/N & VERSION

Think react-native-firebase is great? Please consider supporting all of the project maintainers and contributors by donating via our Open Collective where all contributors can submit expenses. [Learn More]

5 thoughts on “[Android] v6 messaging().setBackgroundMessageHandler not working if app gets killed

  1. The setBackgroundMessageHandler handler has to be created outside of your UI code. E.g:

    // index.js
    
    messaging().setBackgroundMessageHandler(async remoteMessage => {
      console.log('Message handled in the background!', remoteMessage);
    });
    
    function App() {
      useEffect(() => {
        const unsubscribe = messaging().onMessage(async remoteMessage => {
          console.log('FCM Message Data:', remoteMessage.data);
        });
    
        return unsubscribe;
      }, []);
    
      // etc...
    }
    
    export default App;
  2. @Ehesp thanks for your comment! After reading your latest message I checked logcat and I found the cause of the error:
    E/RNFirebaseMsgService: Background messages only work if the message priority is set to 'high'
    So I included the priority set to high along with the data sent to FCM:
    “priority”: “high”
    And I got it working!
    Thank you very much for your help!
    Btw the documentation provided is great but a working example will be highly appreciated by the users.
    If you want I can submit a PR including a sample for them to use as a base for starting implementing the messaging module…

    Cheers,

    Federico

  3. I solved my problem, for those who use react native navigation make sure to call setBackgroundMessageHandler outside of Navigation.events().registerAppLaunchedListener

  4. Issue

    “react”: “16.9.0”,
    “react-native”: “0.61.5”
    “@react-native-firebase/app”: “^6.3.4”,
    “@react-native-firebase/messaging”: “^6.3.4”,
    In Android if the app gets killed messaging().setBackgroundMessageHandler is not invoked.
    So as of v6 notifications functionality is not available anymore I was planning to use react-native-firebase (only the messaging module) to get silent pushes and then using other lib to trigger a local notification to mimic react-native-firebase previous versions behaviour.
    Btw I according to the doc messaging().setBackgroundMessageHandler is only available for Android.
    How can I use react-native-firebase for getting notifications on iOS when the app is closed.
    Thanks in advance.
    Federico

    Project Files

    Javascript

    import messaging from '@react-native-firebase/messaging';
    
    export const firebasePushSetup = async () => {
      await messaging().registerForRemoteNotifications();
    
      const token = await messaging().getToken();
      console.log('TOKEN =', token);
    
      const granted = await messaging().requestPermission();
      console.log('GRANTED =', granted);
    
      messaging().setBackgroundMessageHandler(async remoteMessage => {
        console.log('Message handled in the background!', remoteMessage);
      });
    
      const unsubscribe = messaging().onMessage(async remoteMessage => {
        console.log('FCM Message Data:', remoteMessage.data);
      });
    
      return unsubscribe;
    };
    

    Click To Expand

    I am getting this error: No task registered for key ReactNativeFirebaseMessagingHeadlessTask
    I used priority too

    Solved!!!
    I just added the code :
    messaging().setBackgroundMessageHandler() outside the Navigation.events().registerAppLaunchedListener()

[Android] v6 messaging().setBackgroundMessageHandler not working if app gets killed

Issue

“react”: “16.9.0”,
“react-native”: “0.61.5”
“@react-native-firebase/app”: “^6.3.4”,
“@react-native-firebase/messaging”: “^6.3.4”,

In Android if the app gets killed messaging().setBackgroundMessageHandler is not invoked.
So as of v6 notifications functionality is not available anymore I was planning to use react-native-firebase (only the messaging module) to get silent pushes and then using other lib to trigger a local notification to mimic react-native-firebase previous versions behaviour.
Btw I according to the doc messaging().setBackgroundMessageHandler is only available for Android.
How can I use react-native-firebase for getting notifications on iOS when the app is closed.

Thanks in advance.

Federico


Project Files

Javascript

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

export const firebasePushSetup = async () => {
  await messaging().registerForRemoteNotifications();

  const token = await messaging().getToken();
  console.log('TOKEN =', token);

  const granted = await messaging().requestPermission();
  console.log('GRANTED =', granted);

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

  const unsubscribe = messaging().onMessage(async remoteMessage => {
    console.log('FCM Message Data:', remoteMessage.data);
  });

  return unsubscribe;
};

Click To Expand

package.json:

# N/A

firebase.json for react-native-firebase v6:

# N/A

iOS

Click To Expand

ios/Podfile:

  • I’m not using Pods
  • I’m using Pods and my Podfile looks like:
# N/A

AppDelegate.m:

// N/A

Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->

Environment

Click To Expand

react-native info output:

 OUTPUT GOES HERE
  • Platform that you’re experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you’re using that has this issue:
    • e.g. 5.4.3
  • Firebase module(s) you’re using that has the issue:
    • e.g. Instance ID
  • Are you using TypeScript?
    • Y/N & VERSION

Think react-native-firebase is great? Please consider supporting all of the project maintainers and contributors by donating via our Open Collective where all contributors can submit expenses. [Learn More]

3 thoughts on “[Android] v6 messaging().setBackgroundMessageHandler not working if app gets killed

  1. @Ehesp thanks for your comment! After reading your latest message I checked logcat and I found the cause of the error:
    E/RNFirebaseMsgService: Background messages only work if the message priority is set to 'high'
    So I included the priority set to high along with the data sent to FCM:
    “priority”: “high”
    And I got it working!
    Thank you very much for your help!
    Btw the documentation provided is great but a working example will be highly appreciated by the users.
    If you want I can submit a PR including a sample for them to use as a base for starting implementing the messaging module…

    Cheers,

    Federico

  2. I solved my problem, for those who use react native navigation make sure to call setBackgroundMessageHandler outside of Navigation.events().registerAppLaunchedListener

[Android] v6 messaging().setBackgroundMessageHandler not working if app gets killed

Issue

“react”: “16.9.0”,
“react-native”: “0.61.5”
“@react-native-firebase/app”: “^6.3.4”,
“@react-native-firebase/messaging”: “^6.3.4”,

In Android if the app gets killed messaging().setBackgroundMessageHandler is not invoked.
So as of v6 notifications functionality is not available anymore I was planning to use react-native-firebase (only the messaging module) to get silent pushes and then using other lib to trigger a local notification to mimic react-native-firebase previous versions behaviour.
Btw I according to the doc messaging().setBackgroundMessageHandler is only available for Android.
How can I use react-native-firebase for getting notifications on iOS when the app is closed.

Thanks in advance.

Federico


Project Files

Javascript

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

export const firebasePushSetup = async () => {
  await messaging().registerForRemoteNotifications();

  const token = await messaging().getToken();
  console.log('TOKEN =', token);

  const granted = await messaging().requestPermission();
  console.log('GRANTED =', granted);

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

  const unsubscribe = messaging().onMessage(async remoteMessage => {
    console.log('FCM Message Data:', remoteMessage.data);
  });

  return unsubscribe;
};

Click To Expand

package.json:

# N/A

firebase.json for react-native-firebase v6:

# N/A

iOS

Click To Expand

ios/Podfile:

  • I’m not using Pods
  • I’m using Pods and my Podfile looks like:
# N/A

AppDelegate.m:

// N/A

Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->

Environment

Click To Expand

react-native info output:

 OUTPUT GOES HERE
  • Platform that you’re experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you’re using that has this issue:
    • e.g. 5.4.3
  • Firebase module(s) you’re using that has the issue:
    • e.g. Instance ID
  • Are you using TypeScript?
    • Y/N & VERSION

Think react-native-firebase is great? Please consider supporting all of the project maintainers and contributors by donating via our Open Collective where all contributors can submit expenses. [Learn More]