react-native-firebase
react-native-firebase copied to clipboard
[π] TOO_MANY_REGISTRATIONS error with firebase/messaging ( messaging().getToken() )Β in android.
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.settingsjetifier=truefor Android compatibility? - [ ] I am using the NPM package
jetifierfor 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-firebaseversion you're using that has this issue:e.g. 5.4.3
Firebasemodule(s) you're using that has the issue:e.g. Instance ID
- Are you using
TypeScript?Y/N&VERSION
- π Check out
React Native FirebaseandInvertaseon Twitter for updates on the library.
Hello @nishantSanandiyaAgile, I'm not reproducing this issue on my end. Are you sure your registration code is called only once?
yes sure, the registration code is called only once.
Can you provide a complete sample then so I can have a look?
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.