react-native-firebase
react-native-firebase copied to clipboard
[🐛] Poor error message when setBackgroundMessageHandler is not async
Issue
I was getting a "TypeError: Cannot read property 'then' of undefined, js engine: hermes" on android
and could not figure out what was causing it
function onBackgroundNoticiation(){
}
messaging().setBackgroundMessageHandler(onBackgroundNoticiation);
Just discoverd that setBackgroundMessageHandler expects an async function so resolving that
async function onBackgroundNoticiation(){
}
messaging().setBackgroundMessageHandler(onBackgroundNoticiation);
Gets rid of that problem
Javascript
Click To Expand
package.json:
"@react-native-firebase/app": "^14.11.1",
"@react-native-firebase/crashlytics": "^14.11.1",
"@react-native-firebase/messaging": "^14.11.1",
firebase.json for react-native-firebase v6:
# N/A
iOS
N/A
Android
Click To Expand
Have you converted to AndroidX?
- [x ] my application is an AndroidX application?
- [ ] I am using
android/gradle.settingsjetifier=truefor Android compatibility? - [ ] I am using the NPM package
jetifierfor 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
RN: 0.69.1
- Platform that you're experiencing the issue on:
- [ ] iOS
- [ x] Android
- [ ] iOS but have not tested behavior on Android
- [ ] Android but have not tested behavior on iOS
- [ ] Both
react-native-firebaseversion you're using that has this issue:- 14.11.1
Firebasemodule(s) you're using that has the issue:- messaging
- Are you using
TypeScript?- N
- 👉 Check out
React Native FirebaseandInvertaseon Twitter for updates on the library.
Hi there! How do you propose to solve the issue? In my experience this is what typescript is for, really. You would receive a type error for attempting to send in a function with the wrong signature. We publish typescript types. What would you propose ?
Maybe be able to catch the error and give a reasonable error message?
I'm not sure how hard that would be, when I looked deeper in the docs it was easy to tell the issue, but I had the error randomly returning for months and could not figure out because it was not descriptive.
It also seems non-intuitive that this would cause a crash in the first place, I don't see why it has to be asynchronous especially since its not returning anything anyways.
android headless mode has a number of eccentricities and I assume (though do not know for certain, sorry) that it is related to them
I was thinking more about examining available methods on the Object prototype to see if then was a method, that is, reflectively inspect the passed in object.
That way you've got fail fast (when the method is called, so you see it in development) instead of fail-late where you get a crash and have to pick up the pieces
Something like this https://stackoverflow.com/questions/38508420/how-to-know-if-a-function-is-async
I just took a look at the code and I see that it is just getting passed back in to react native.
I do believe this would resolve the problem completely
AppRegistry.registerHeadlessTask("ReactNativeFirebaseMessagingHeadlessTask", () => {
if (!backgroundMessageHandler) {
// eslint-disable-next-line no-console
console.warn(
'No background message handler has been set. Set a handler via the "setBackgroundMessageHandler" method.'
);
return () => Promise.resolve();
}
return async (remoteMessage) => {
return await backgroundMessageHandler(remoteMessage);
};
});
But not sure if that is good standards are anything.
I do see it is my fault for just ignoring the types on this module out of laziness, so I could be fine with a no-fix here, and hopefully just the existence of this bug will help other people who are running into a similar problem.
I think you are getting that error because the DataModel.getList() doesn't return a Promise so you can have access to Promise then chain. All you have to do is to add a return keyword in the getList before the fetch function so the getList method can return a resolve Promise instead of undefined
Hello 👋, to help manage issues we automatically close stale issues.
This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?
This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.