invertase / react-native-firebase

πŸ”₯ A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.66k stars 2.21k forks source link

iOS push notifications not working. No errors. #1890

Closed ccorcos closed 5 years ago

ccorcos commented 5 years ago

Issue

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.


Project Files

iOS

ios/Podfile:

# N/A

AppDelegate.m:

// N/A

Android

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


Think react-native-firebase is great? Please consider supporting the project with any of the below:

stale[bot] commented 5 years ago

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.

ahmadghizzawi commented 5 years ago

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().

stale[bot] commented 5 years ago

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.

ManigandanRaamanathan commented 5 years ago

@Stale[bot]

ManigandanRaamanathan commented 5 years ago

any update on this issue?

stale[bot] commented 5 years ago

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.

ahmadghizzawi commented 5 years ago

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.

stale[bot] commented 5 years ago

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.

stale[bot] commented 5 years ago

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.