Upon pushing some data to firebase in react native I get the following error that my firebase database URL is undefined. I am using the android emulator here. Maybe I am missing something I need to push to firebase in my android build gradle file?
I am currently also using firebase auth for this app it it does work perfectly fine but I had to modify the android build gradle. I'm wondering if this could be a similar issue? If so does anyone know what I need to add?
I'm new to react-native sorry :(
Below is my config and my firebase app initialization
sendNoteToDatabase = () => {
const firebaseConfig = {
apiKey: 'my api key',
authDomain: '*********-37824.firebaseapp.com',
databaseURL: 'https://******-37824.firebaseio.com/',
projectId: '******-23288'
}
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
const note = {
title: "this.state.title",
who: "this.state.who",
time: "this.state.time"
}
firebase.database().ref('Users/').set(note).then((data) => {
//success callback
console.log('data ', data)
}).catch((error) => {
//error callback
console.log('error ', error)
})
}
I have checked the URL and I am using a realtime database. I'm not sure if this is an issue with my code structure or a problem with android?
using Franks Advice I was able to solve it by initializing the firebase app using the componentDidMount hook
componentDidMount(){
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
}
Related
I am trying to implement a simple button to open the notification settings page for the app.
On previous expo SDKs, this code was used for opening the Android page:
const pkg = Constants.manifest.releaseChannel
? Constants.manifest.android.package // When published, considered as using standalone build
: 'host.exp.exponent'
IntentLauncher.startActivityAsync(IntentLauncher.ACTION_APP_NOTIFICATION_SETTINGS, {
data: 'package:' + pkg
})
But this now gives an error:
No Activity found to handle Intent { act=android.settings.APP_NOTIFICATION_SETTINGS dat=package: }
I've tried looking on Stackoverflow and Expo forums, but everything is using the old SDKs.
There is a much simpler way to do it using the Linking module.
import { Linking } from 'react-native';
const handleTurnNotificationsOn = async () => {
await Linking.openSettings()
}
This worked for me.
https://reactnative.dev/docs/linking#sendintent-android
const pkg = Constants.manifest.releaseChannel
? Constants.manifest.android.package : 'host.exp.exponent'
Linking.sendIntent("android.settings.APP_NOTIFICATION_SETTINGS", [
{
key: "android.provider.extra.APP_PACKAGE",
value: pkg,
},
]);
In my project, I was using flutter 3.0.3. Once I updated to 3.0.4 and updated dependencies, suddenly when I launched the app on android, the splash screen is showing continuously(never going to the main app). I tried by moving to my previous commit, downgrading flutter but to no avail.
This happened before but I luckily fixed it by upgrading kotlin jdk to 8(was 7 before).
I am using flutter_native_splash library. But it looks it doesn't have any impact on my project.
This question talks about only release mode. But, in my case it is happening both release and debug modes. I would really appreciate your help on this!
My main function:
void mainCommon() {
WidgetsFlutterBinding.ensureInitialized();
SentryFlutter.init(
(options) => options.dsn = Config.sentryDsn,
appRunner: () async {
await Firebase.initializeApp();
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
final GlobalKey<ScaffoldMessengerState> messengerKey = GlobalKey<ScaffoldMessengerState>();
await BlocOverrides.runZoned(
() async => App(
api: Api(Config.grpcChannel),
database: await Config.db,
appDirectory: await getApplicationDocumentsDirectory(),
notificationManager: await NotificationManager.init(Config.grpcChannel),
messengerKey: messengerKey,
),
blocObserver: AppBlocObserver(errorCallback: BlocErrorHandler(messengerKey).errorCallback),
);
},
);
}
This is not for all cases. But, upgrading firebase_core and firebase_messaging could solve for some cases. In my case, it solved the problem for the first. But, once I upgraded all my other packages, it occurred again.
Android studio sucks. When I used Visual Studio Code, the exception was thrown in main where I initialised firebase. But in android, it ignored exception so I wasted a lot of time finding where the problem was.
To see the error in Android studio, you need to get mainCommon in try/catch bloc:
try {
await Firebase.initializeApp();
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
runApp(App(
api: Api(Config.grpcChannel),
database: await Config.db,
appDirectory: await getApplicationDocumentsDirectory(),
notificationManager: await NotificationManager.init(Config.grpcChannel),
));
} catch (e) {
logger.e("Got exception in mainCommon: $e");
rethrow;
}
i'm using a very simple code and fetching data from firestore
import firebase from 'firebase/app';
import 'firebase/firestore'
const firebaseApp = firebase.initializeApp({
apiKey: "...",
authDomain: "...",
....
});
const db = firebaseApp.firestore();
export default db;
but i keep getting this error
[2021-06-05T00:58:41.274Z] #firebase/firestore: Firestore (8.6.5): Could not reach Cloud Firestore backend. Connection failed 1 times.
Most recent error: FirebaseError: [code=permission-denied]:
Permission denied on resource project.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
i do have a very fast internet connection
my clock is also synced with the standard time
Now, i have no clue why is this happening?
please someone help me out!!!
I was facing the same issue. The connection was working on some environments, but on my client's corporate network it wasn't.
After a loong research on the internet, I found an issue on Github talking about it.
Here's what worked for me:
const firestoreDB = initializeFirestore(firebaseApp, {
experimentalForceLongPolling: true, // this line
useFetchStreams: false, // and this line
})
Here i'm using firebase v9. For firebase v8 it's something like:
firebase().firestore().settings({
experimentalForceLongPolling: true, // this line
useFetchStreams: false, // and this line
})
i was able to resolve this issue by using my credential directly where i initializeApp my firebase config, i was previously calling them from my env file i think my app was not getting the env before
Make sure your environment is pointing to the real firestone database and not the Firestore Emulator. When I came across this same issue, that was the reason.
I'm using an Angular framework so I had to comment out those environment references in my app.module.ts file.
#NgModule({
declarations: [AppComponent, InformUserComponent],
entryComponents: [InformUserComponent],
imports: [
BrowserModule,
IonicModule.forRoot(),
AngularFireModule.initializeApp(environment.firebaseConfig),
AppRoutingModule,
ServiceWorkerModule.register('ngsw-worker.js', {
enabled: environment.production,
// Register the ServiceWorker as soon as the app is stable
// or after 30 seconds (whichever comes first).
registrationStrategy: 'registerWhenStable:30000'
}),
],
providers: [AuthService, { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
// {
// provide: USE_FIRESTORE_EMULATOR, useValue: environment.useEmulators ?
// ['localhost', 8080] : undefined
// },
// {
// provide: USE_FUNCTIONS_EMULATOR, useValue: environment.useEmulators ?
// ['localhost', 5001] : undefined
// },
],
bootstrap: [AppComponent],
})
export class AppModule { }
I had the same issue and tried to use the #atunde arisekola approach. If you can solve this problem by using direct credentials in your firebaseConfig variable, consider to check whether your .env.local or any other .env you are using is located in the root directory. In my case had firebaseConfig.ts and .env.local in the same directory, and thus the error occured. It is recommended to have.env in the root of your App.
I have faced this issue as well with angular fire.
What I have changed is to add firestore settings to the provider of app module:
According to the GitHub issues discussion, I think you can try experimentalAutoDetectLongPolling first.
And option useFetchStreams is unnecessary except for users who are using very old browser versions.
import { SETTINGS as FIRESTORE_SETTINGS } from '#angular/fire/firestore';
providers:[
{
provide: FIRESTORE_SETTINGS,
useValue: { experimentalAutoDetectLongPolling: true, merge: true },
}
]
and you can use mitmproxy to reproduce the firebase errors.
I am using react-native to build an android app while authentication is done using firebase. I am using the following libraries :
#react-native-firebase/app
#react-native-firebase/auth
The authentication is working perfectly fine for signInWithEmailAndPassword() but not with the phone no :
The below is the function :
signInWithPhoneNumber = () => {
auth().signInWithPhoneNumber('+91 1234 567 890')
.then(confirmationResult => {
this.setState({
confirm: confirmationResult
})
})
.catch(function (error) {
console.error(error);
});
}
It's not going into the .then() and my screen for the emulator is getting stuck with blank black screen.
UPDATE (May 20, 2021)
Seems line the issue is fixed in the latest version.
This is a known issue in the firebase phone auth sdk.
Ref: https://github.com/firebase/firebase-android-sdk/issues/2164
Until Google fixes it, add the following line in your build.gradle
implementation "androidx.browser:browser:1.2.0"
I'm using ionic 3 for android app but got error in google plus login with firebase. Integrated ionic and cordova g+ plugins and my code is
gPlusLogin(){
this.nativeGLogin();
}
async nativeGLogin(){
try {
const gplusUser = await this.googlePlus.login({
'webClientId': '235*********ie.apps.googleusercontent.com',
'offline': true
})
return await this.afAuth.auth.signInWithCredential(firebase.auth.GithubAuthProvider.credential(gplusUser.idToken))
} catch (error) {
this.errorAlert(error)
}
}
You're using GithubAuthProvider with your signInWithCredential when you meant to use Google sign in, that's probably the problem.