Hello there, I have a rather tricky bug report that I'm not completely able to reproduce. However, I ironically have a fair degree of confidence that this is a bug and how to fix it.
I have this rather straightforward code inside by iOS app:
async function registerPushNotifications() {
const enabled = await firebase.messaging().hasPermission()
if (!enabled) {
try {
await firebase.messaging().requestPermission()
} catch (error) {
// permission denied
return
}
}
const token = await firebase.messaging().getToken()
return token
}
I think it makes sense and the documentation seems to say that this is a correct way to use the api.
If the user has not already granted permissions, then you can prompt them to do so, as follows
Well I've notices that for some people (in our office) push notifications stopped working. On one of the phones, we deleted the app and reinstalled it and that fixed the problem. This person also got a new token. For other person, we "upgraded" the app by running the app in Xcode without deleting it first. The token did not change the the problem was still there.
When I send a message to this token directly from the firebase API, I don't get any errors when I send the message and I don't see any errors on the device. It just silently fails.
Anyways, I recently migrated this application from Cordova and I remembered a bug where you have to call grantPermission() every time the app is booted regardless of whether hasPermission is true or not. It turns out that this was my problem (again!) when using this library.
Well, it turns out that if you don't call requestPermission() every time you open the app, at some point, push notifications will stop working.
So my new code which works and healed itself without having to delete the app or create a new token was this:
async function registerPushNotifications() {
try {
await firebase.messaging().requestPermission()
} catch (error) {
// permission denied
return
}
const token = await firebase.messaging().getToken()
return token
}
So there we go, figured it out. At the very least, I think you should make it clear in the documentation that you must call requestPermission() before getToken(). I don't know why... but it certainly was the problem in my case.
ios/Podfile:# N/A
AppDelegate.m:// N/A
android/build.gradle:// N/A
android/app/build.gradle:// N/A
android/settings.gradle:// N/A
MainApplication.java:// N/A
AndroidManifest.xml:<!-- N/A -->
ADD_SOMETHING_HERE e.g. iOS 10 or Android API 28N/AN/AN/AADD_SOMETHING_HERE e.g. Xcode 10, Android Studio 3.2React Native version:ADD_SOMETHING_HEREReact Native Firebase library version:ADD_SOMETHING_HEREFirebase module(s) you're using that has the issue:TypeScript?N/AExpoKit?ExpoKitN/A
Think react-native-firebase is great? Please consider supporting the project with any of the below:
React Native Firebase and Invertase on TwitterHello 馃憢, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.
Hey @ccorcos. Thank you for sharing this! We were facing similar issues as well that was related to the fact that we weren't calling firebase.messaging().requestPermission() before await firebase.messaging().getToken().
Hello 馃憢, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.
@Stale[bot]
any update on this issue?
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 the community's attention?
This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.
In our case, the issue was caused due to incorrect initialisation of Instabug. Once we fixed that, the issue never happened again either on iOS or Android.
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 the community's attention?
This issue will be closed in 15 days if no further activity occurs.
Thank you for your contributions.
Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.
Most helpful comment
Hey @ccorcos. Thank you for sharing this! We were facing similar issues as well that was related to the fact that we weren't calling
firebase.messaging().requestPermission()beforeawait firebase.messaging().getToken().