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

[πŸ›] TOO_MANY_REGISTRATIONS error with firebase/messaging ( messaging().getToken() )Β in android.

Open nishantSanandiyaAgile opened this issue 1 year ago β€’ 4 comments

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:

# 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:
# N/A

AppDelegate.m:

// 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:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->

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

nishantSanandiyaAgile avatar Jul 19 '24 06:07 nishantSanandiyaAgile

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

Lyokone avatar Jul 19 '24 06:07 Lyokone

yes sure, the registration code is called only once.

nishantSanandiyaAgile avatar Jul 19 '24 06:07 nishantSanandiyaAgile

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

Lyokone avatar Jul 29 '24 13:07 Lyokone

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.

github-actions[bot] avatar Aug 26 '24 13:08 github-actions[bot]