React-native-firebase: 馃敟 Failed to grant permission (iOS Messaging)

Created on 26 Jan 2019  路  22Comments  路  Source: invertase/react-native-firebase


Issue



Since some updates ago of this library I'm experiencing some issues related with the grant of permissions for notifications on iOS. The thing is I have a function like this:

export const notificationsSetup = () => async (dispatch, getState) => {
   // Permiso para recibir notificaciones
   const enabled = await firebase.messaging().hasPermission();

   if (!enabled){
      try {
         await firebase.messaging().requestPermission();
      } catch (error) {
         console.error(error);
      }
   }

   const fcmToken = await firebase.messaging().getToken();

   if (fcmToken) {
      dispatch(FCMtokenActions(fcmToken));
   }
}

This function is called every time the user sign in on the app or the FCM token reloads itself. The problem is that in debug, when the alert to grant notification permissions appear, even if you click "Allow" this message appears:

simulator screen shot - iphone xs - 2019-01-26 at 18 46 58

I think the notifications work well because I did some testing and they arrive to the phone even after this error appears. But seeing this error didn't make me so confident.


Project Files






iOS

ios/Podfile:

  • [ ] I'm not using Pods
  • [x] I'm using Pods and my Podfile looks like:
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'MYAPP' do
    # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
    # use_frameworks!

    # REACT NATIVE DEPENDENCIES

    # Firebase
    pod 'Firebase/Core', '~> 5.15.0'
    pod 'Firebase/Auth', '~> 5.15.0'
    pod 'Firebase/Messaging', '~> 5.15.0'
    pod 'Firebase/Storage', '~> 5.15.0'

    # Crashlytics
    pod 'Fabric', '~> 1.7.13'
    pod 'Crashlytics', '~> 3.10.7'

    # MY DEPENDENCIES
end

AppDelegate.m:

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import "AppDelegate.h"
#import <CodePush/CodePush.h>

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import "RNGoogleSignin.h"
#import <Firebase.h>
#import "RNFirebaseNotifications.h"
#import "RNFirebaseMessaging.h"

@implementation AppDelegate

// application openURL [...]

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [[RNFirebaseNotifications instance] didReceiveLocalNotification:notification];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
    [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [FIRApp configure];
    [RNFirebaseNotifications configure];

        // MY CODE

  return YES;
}

@end

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:

    • [x] 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:

    • iOS 12

  • Operating System:

    • [x] MacOS, version: 10.14.2

    • [ ] Windows, version: N/A

    • [ ] Other, please specify: N/A

  • Build Tools:

    • Xcode 10

  • React Native version:

    • Tested on 0.56, 0.57, 0.57.8, 0.58.1

  • React Native Firebase library version:

    • Latest version (5.2.1)

  • 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

    • [ ] Invites

    • [ ] Instance ID

    • [x] Notifications

    • [ ] Performance Monitoring

    • [ ] Realtime Database

    • [ ] Remote Config

    • [ ] Storage

  • Are you using TypeScript?

    • [x] 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

same issue, after pressing Allow it throws an error Failed to grant permission.

All 22 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.

mmm.. I also have this issue

Pretty much the same code

I get the same without using Pods. Using Carthage

I am also facing the same issue, And "not" too much suggestions and help article related to this I am finding. Please help me out here.

I'm also having this issue. Any news about how to fix it?

@Andresain123 I removed the version from the podfile, and I'm not having the failed to grand permission issue, but I still not able to get notifications...

    # Firebase
    pod 'Firebase/Core' #, '~> 5.15.0'
    pod 'Firebase/Messaging' #, '~> 5.15.0'

after removing the version I get these versions:

Installing Firebase 5.16.0 (was 5.15.0)
Installing FirebaseAnalytics 5.5.0 (was 5.4.0 and source changed to `https://github.com/CocoaPods/Specs.git` from `https://github.com/cocoapods/specs.git`)
Using FirebaseAnalyticsInterop (1.1.0)
Installing FirebaseCore 5.2.0 (was 5.1.10 and source changed to `https://github.com/CocoaPods/Specs.git` from `https://github.com/cocoapods/specs.git`)
Installing FirebaseInstanceID 3.4.0 (was 3.3.0 and source changed to `https://github.com/CocoaPods/Specs.git` from `https://github.com/cocoapods/specs.git`)
Installing FirebaseMessaging 3.3.0 (was 3.2.2 and source changed to `https://github.com/CocoaPods/Specs.git` from `https://github.com/cocoapods/specs.git`)

I'm also having this issue. This is my code:

checkForNotificationsPermission() {
firebase.messaging().hasPermission().then(granted => {
            if (granted) {
                firebase.messaging().getToken().then(token => {
                    if (token) 
                        this.setState({token});
                    })
            } else {
                firebase.messaging().requestPermission().then(() => {
                        this.checkForNotificationsPermission();
                }).catch(err => {
                    //This is where I get the error:  "failed to grant permissions" (on err.message)
                })
            }
        })
}

Also having this issue, with this code:

const requestNotificationPermission = async () => {
  try {
    if (!await firebase.messaging().hasPermission()) {
      await firebase.messaging().requestPermission();
    }
  } catch (e) {
    // error happens here
  }
};

Just wanted to let everyone know.
I've been searching for a solution for weeks and didn't find any, so I made a workaround.
After requesting for permission, whether the user allowed or denied, call another function named "checkAgain()" and do this:

async checkAgain() {
        const enabled = await firebase.messaging().hasPermission();
        if (enabled) {
            this.getToken();
        } else {
            alert("We can't proceed without permission to send you notifications");
        }
    } 

With this code I don't show the false "notification permission failed" error message.

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.

I have same issue

same issue, after pressing Allow it throws an error Failed to grant permission.

same issue here..

I've refactored my app for react-native@61 with Cocoa Pods (I was using Carthage before) and I'm still getting this false error so I think we can eliminate that element from repro-ing this issue

onTokenRefresh listener rescue me
firebase.messaging().onTokenRefresh

onTokenRefresh listener rescue me
firebase.messaging().onTokenRefresh

@peterng014 can you explain how this saved you? I'm still seeing this error and it's intermittent and I can't figure out exactly why it happens.

Could this be iOS refusing to show the native prompt after the first time (and it was refused)?

The first time your app makes this authorization request, the system prompts the user to grant or deny the request and records the user鈥檚 response. Subsequent authorization requests don鈥檛 prompt the user.
https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications

@tucoinfo certainly could be that - I would if at all possible use react-native-permissions to handle all this stuff. Has flowcharts showing possible permission outcomes, is super focused on staying up to date etc

HI!, same issue, after pressing Allow

"react-native": "0.59.9",
"react-native-firebase": "5.5.7",
Platform: IOS

小薪懈屑芯泻 褝泻褉邪薪邪 2020-05-22 胁 16 43 07

@rkolos -

  1. images are terrible debugging tools on github, use text enclosed in triple-backticks
  2. we are no longer supporting react-native-firebase v5 so there will be no updates here - v6 should handle things well though
  3. use react-native-permissions if at all possible to manage your permissions in a single coherent way
Was this page helpful?
0 / 5 - 0 ratings