react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

πŸ”₯ Failed to grant permission (iOS Messaging)

Open andresain123 opened this issue 7 years ago β€’ 22 comments

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:

andresain123 avatar Jan 26 '19 18:01 andresain123

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] avatar Feb 23 '19 19:02 stale[bot]

mmm.. I also have this issue

unicornRainbows avatar Mar 01 '19 10:03 unicornRainbows

Pretty much the same code

unicornRainbows avatar Mar 01 '19 10:03 unicornRainbows

I get the same without using Pods. Using Carthage

CapitanRedBeard avatar Mar 11 '19 15:03 CapitanRedBeard

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.

swapanil avatar Mar 13 '19 06:03 swapanil

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

gabrielperales avatar Mar 21 '19 11:03 gabrielperales

@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`)

gabrielperales avatar Mar 21 '19 16:03 gabrielperales

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)
                })
            }
        })
}

HaCStudio avatar Apr 14 '19 18:04 HaCStudio

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
  }
};

philnova avatar Apr 18 '19 16:04 philnova

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.

HaCStudio avatar Apr 25 '19 08:04 HaCStudio

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] avatar May 23 '19 09:05 stale[bot]

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.

stale[bot] avatar Jun 07 '19 09:06 stale[bot]

I have same issue

bmp123 avatar Jun 15 '19 15:06 bmp123

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

ErrorPro avatar Jul 08 '19 20:07 ErrorPro

same issue here..

jackyleehong avatar Aug 29 '19 04:08 jackyleehong

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

CapitanRedBeard avatar Nov 25 '19 18:11 CapitanRedBeard

onTokenRefresh listener rescue me firebase.messaging().onTokenRefresh

peterng014 avatar Dec 12 '19 04:12 peterng014

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.

thegregthomp avatar Jan 02 '20 17:01 thegregthomp

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’s response. Subsequent authorization requests don’t prompt the user. https://developer.apple.com/documentation/usernotifications/asking_permission_to_use_notifications

tucoinfo avatar Feb 13 '20 08:02 tucoinfo

@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

mikehardy avatar Feb 13 '20 15:02 mikehardy