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.63k stars 2.2k forks source link

[🐛] TOO_MANY_REGISTRATIONS error with firebase/messaging ( messaging().getToken() ) in android. #7917

Open nishantSanandiyaAgile opened 1 month ago

nishantSanandiyaAgile commented 1 month ago

Issue

Describe your issue here


Project Files

import DeviceInfo from 'react-native-device-info';
import {PERMISSIONS} from 'react-native-permissions';
import messaging from '@react-native-firebase/messaging';

import {PLATFORM_OS} from '~/helper';

import {NotificationUtils} from './Notification';
import {requestSinglePermissionHandler} from './PermissionsHandler';
import {Utility} from './Utility';

/**
 * The function `getFCMTokenAndroid` retrieves the FCM token for Android devices, handling different
 * Android versions and requesting the necessary permissions.
 * @returns The function `getFCMTokenAndroid` returns a Promise that resolves to a string value, which
 * is the FCM token obtained from the device.
 */
const getFCMTokenAndroid = async (): Promise<string> => {
  let fcmToken = 'not found';
  try {
    const AndroidVersion: number = +DeviceInfo.getSystemVersion().split('.')[0];
    if (AndroidVersion >= 13) {
      const result = await requestSinglePermissionHandler(
        PERMISSIONS.ANDROID.POST_NOTIFICATIONS,
      );
      if (result) {
        await NotificationUtils.createNotificationChannel();
      }
    } else {
      await NotificationUtils.createNotificationChannel();
    }
    fcmToken = await messaging().getToken();
  } catch (error: any) {
    Utility.log('Error getting FCM Token in android : ', error);
  }
  return fcmToken;
};

/**
 * The function `getFCMTokenIOS` is an asynchronous function that requests permission from the user to
 * send notifications on iOS and retrieve the FCM token if permission is granted.
 * @returns The function `getFCMTokenIOS` returns a Promise that resolves to a string.
 */
const getFCMTokenIOS = async (): Promise<string> => {
  let fcmToken = 'not found';
  const isEmulator = await DeviceInfo.isEmulator();
  if (isEmulator) {
    fcmToken = 'iOS Simulator';
    return fcmToken;
  }
  try {
    await messaging().requestPermission();
    // const authStatus = await messaging().requestPermission();
    // const enabled =
    //   authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
    //   authStatus === messaging.AuthorizationStatus.PROVISIONAL;
    // if (enabled) {
    //   fcmToken = await messaging().getToken();
    // }
    fcmToken = await messaging().getToken();
  } catch (error) {
    Utility.log('Error getting FCM Token in iOS : ', error);
  }
  return fcmToken;
};

/**
 * The function `getFCMToken` returns the FCM token for either Android or iOS devices.
 * @returns The function `getFCMToken` returns a promise that resolves to a string.
 */
export const getFCMToken = async (): Promise<string> => {
  let fcmToken = 'not found';

  if (PLATFORM_OS === 'android') {
    fcmToken = await getFCMTokenAndroid();
  } else {
    fcmToken = await getFCMTokenIOS();
  }
  return fcmToken;
};

Javascript

Click To Expand

#### `package.json`: ```json # N/A ``` #### `firebase.json` for react-native-firebase v6: "@react-native-firebase/app": "^18.8.0", "@react-native-firebase/messaging": "^18.8.0",

iOS

Click To Expand

#### `ios/Podfile`: - [ ] I'm not using Pods - [x] I'm using Pods and my Podfile looks like: ```ruby # N/A ``` #### `AppDelegate.m`: ```objc // N/A ```


Android

Click To Expand

#### Have you converted to AndroidX? - [ ] my application is an AndroidX application? - [ ] I am using `android/gradle.settings` `jetifier=true` for Android compatibility? - [ ] I am using the NPM package `jetifier` for react-native compatibility? #### `android/build.gradle`: ```groovy // N/A ``` #### `android/app/build.gradle`: ```groovy // N/A ``` #### `android/settings.gradle`: ```groovy // N/A ``` #### `MainApplication.java`: ```java // N/A ``` #### `AndroidManifest.xml`: ```xml ```


Environment

Click To Expand

**`react-native info` output:** ``` OUTPUT GOES HERE ``` - **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 - **`react-native-firebase` version you're using that has this issue:** - `e.g. 5.4.3` - **`Firebase` module(s) you're using that has the issue:** - `e.g. Instance ID` - **Are you using `TypeScript`?** - `Y/N` & `VERSION`


Lyokone commented 1 month ago

Hello @nishantSanandiyaAgile, I'm not reproducing this issue on my end. Are you sure your registration code is called only once?

nishantSanandiyaAgile commented 1 month ago

yes sure, the registration code is called only once.

Lyokone commented 1 month ago

Can you provide a complete sample then so I can have a look?

github-actions[bot] commented 1 week 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 attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.