React-native-firebase: iOS push notifications not working. No errors.

Created on 9 Feb 2019  路  9Comments  路  Source: invertase/react-native-firebase


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:

  • [ ] I'm not using Pods
  • [x] I'm using Pods and my Podfile looks like:
# 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

  • 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

  • If known, the version of the platform are you experiencing the issue on:

    • ADD_SOMETHING_HERE e.g. iOS 10 or Android API 28

  • Operating System:

    • [ ] MacOS, version: N/A

    • [ ] Windows, version: N/A

    • [ ] Other, please specify: N/A

  • Build Tools:

    • ADD_SOMETHING_HERE e.g. Xcode 10, Android Studio 3.2

  • React Native version:

    • ADD_SOMETHING_HERE

  • React Native Firebase library version:

    • ADD_SOMETHING_HERE

  • Firebase module(s) you're using that has the issue:

    • [x] N/A

    • [ ] Authentication

    • [ ] Analytics

    • [ ] Cloud Firestore

    • [ ] Cloud Messaging (FCM)

    • [ ] Crashlytics

    • [ ] Dynamic Links

    • [ ] Functions Callable

    • [ ] In App Messaging

    • [ ] Indexing

    • [ ] Invites

    • [ ] Instance ID

    • [ ] ML Kit

    • [ ] Notifications

    • [ ] Performance Monitoring

    • [ ] Realtime Database

    • [ ] Remote Config

    • [ ] Storage

  • Are you using TypeScript?

    • [ ] No

    • [ ] Yes, version: N/A

  • Are you using Expo, e.g. ExpoKit?

    • [x] No

    • [ ] Yes, I've _not_ ejected

    • [ ] Yes, but I have ejected to ExpoKit

    • [ ] Yes, but I have ejected to vanilla React Native

    • Expo version: N/A




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

Stale

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() before await firebase.messaging().getToken().

All 9 comments

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.

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.

Was this page helpful?
0 / 5 - 0 ratings