question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-native-firebase onNotificationOpenedApp method not firing

See original GitHub issue

Bug report

Summary

I’m using react-native-firebase and have done some research, I found that others are also experiencing the same problem:

I previously used react-native-splash-screen on this project, and onNotificationOpenedApp worked on android but not on ios, then I did some digging and found this comment saying to use react-native-bootsplash instead, so I switched to this library but now it no longer works on both android and ios. So I thought it might be related to this library…

on my index.js I have:

import 'setDefaults';

import { AppRegistry } from 'react-native';
import { name as appName } from './app.json';
import Main from 'Main';

AppRegistry.registerComponent(appName, () => Main);

Then the Main.js:

import React from 'react';
import App from 'App';

function Main ({ isHeadless }) {
  if (isHeadless) return null;
  return <App />;
}

export default React.memo(Main);

On App.js,

Outside the App component:

messaging().setBackgroundMessageHandler(async notif => {
  console.log('background notif!', notif);
});

Then in the App component I have this effect:

React.useEffect(() => {
  if (!wasNotifsAllowed) return;

  (async () => {
    const token = await iid().getToken();
    console.log('token', token);
  })();

  const listeners = [
    messaging().onMessage(async notif => {
      console.log('foreground notif!', notif);
    }),

    messaging().onNotificationOpenedApp(async notif => {
      console.log('opened notif!', notif);
    }),

    messaging().onTokenRefresh(async token => {
      console.log('new token', token);
    })
  ];

  return () => {
    listeners.forEach(unsubscribe => {
      unsubscribe();
    });
  };
}, [wasNotifsAllowed]);

I handle the wasNotifsAllowed on App.js as well on the following effect:

React.useEffect(() => {
  (async () => {
    const authorizationStatus = await messaging().requestPermission({
      sound: true,
      badge: true
    });

    const wasNotifsAllowed =
      authorizationStatus === messaging.AuthorizationStatus.AUTHORIZED ||
      authorizationStatus === messaging.AuthorizationStatus.PROVISIONAL;

    setState({ wasNotifsAllowed });
  })();
}, []);
  • messaging().onMessage(callback) works well.
  • messaging().setBackgroundMessageHandler(callback) also works well on android, I’m seeing console logs, on ios, I don’t see the console logs but I am able to receive the notification even when the app is on killed state.

My main/AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.shine.satellite">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
        android:exported="true"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data
              android:host="<url-here>"
              android:pathPrefix="/android/${applicationId}/callback"
              android:scheme="${applicationId}" />
        </intent-filter>
      </activity>

      <activity
        android:name="com.zoontek.rnbootsplash.RNBootSplashActivity"
        android:theme="@style/BootTheme"
        android:launchMode="singleTask">
        <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>

      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />

      <activity
        android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
        android:theme="@style/Base.Theme.AppCompat">
      </activity>
    </application>

</manifest>

Environment info

react-native info output:

System:
    OS: macOS 11.0.1
    CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
    Memory: 25.56 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.2.0 - /var/folders/zg/f6_98ss10lj5b841clyhsxlh0000gn/T/yarn--1608166773585-0.5611083345771732/node
    Yarn: 1.22.10 - /var/folders/zg/f6_98ss10lj5b841clyhsxlh0000gn/T/yarn--1608166773585-0.5611083345771732/yarn
    npm: 7.0.8 - ~/.nvm/versions/node/v15.2.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.0 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.3, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
    Android SDK:
      API Levels: 24, 26, 28, 29, 30
      Build Tools: 28.0.3, 29.0.2, 30.0.2
      System Images: android-24 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom, android-30 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.6953283
    Xcode: 12.3/12C33 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_275 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: ^16.14.0 => 16.14.0
    react-native: 0.63.3 => 0.63.3
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Library version: x.x.x

Steps to reproduce

  1. Init React-Native app
  2. Configure the app to use firebase
  3. Configure the app to use react-native-bootsplash

Describe what you expected to happen:

  1. react-native-bootsplash should work with firebase.

Reproducible sample code

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
aprilmintacpinedacommented, Dec 17, 2020

Yeah I got it now @zoontek, I was mistaken, I was killing the app instead of putting it on the background. It works flawlessly. Thanks 🙇🏻

1reaction
mikehardycommented, Dec 17, 2020

I’m obviously heavily involved in react-native-firebase, I wrote the comments on the old splash screen issue after initially figuring out and posting the root cause, and recommend using this library plus I help @zoontek here - especially on cross over issues like this. I saw the comment you left on the issue on react-native-firebase repo linking here and I just have to say: I don’t see this problem in my app.

I recommend on the linked issue that you make a completely clean demo starting from scratch (e.g. https://github.com/mikehardy/rnfbdemo/blob/master/make-demo.sh) where you try to reproduce it, and I further recommend that if you can reproduce it that you then post that demo on github so we may see it.

Last time this happened someone had integrated (but not mentioned!) that they had integrated onesignal or some other package that was eating the Intent extras and not mentioned it, which wasted a lot of time. I won’t investigate this without a reproduction I can fully see

Read more comments on GitHub >

github_iconTop Results From Across the Web

messaging().onNotificationOpenedApp is never triggered ...
Issue I successfully registered a background handler with setBackgroundMessageHandler and everything works fine. Now I'm trying to handle notification tap ...
Read more >
messaging().onNotificationOpenedApp is never triggered ...
Now I'm trying to handle notification tap when the app is in background/quit and I'm using onNotificationOpenedApp / getInitialNotification .
Read more >
messaging | React Native Firebase
When the app is opened from iOS notifications settings from a quit state, this method will return true or false if the app...
Read more >
Push Notifications (V1 legacy) | Stream Chat - GetStream.io
Easiest way to integrate push notifications in your Chat applications is using Firebase. React Native Firebase is the officially recommended collection of ...
Read more >
How to Add Firebase Push Notifications to Your React Native ...
yarn add @react-native-firebase/messaging npm i @react-native-firebase/messaging ... //import the function that you create on pushnotificationHelper.ts file
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found