So guys I have my app working 100% with Firebase and react-native but the problem is if I try to disconnect the internet the app don't get cached information.
On official Firebase says that firebase works with local storage cache and provides off-line information, but how?
If you are using JavaScript SDK, it will not work on mobile apps.
To achieve this feature, you have to add react-native-firebase, which is an abstraction of JavaScript SDK to both iOS and Android SDK.
https://rnfirebase.io/
After adding it to your project, you can setup the following config:
Android
Add FirebaseDatabase.getInstance().setPersistenceEnabled(true); inside
your MainApplication.java files onCreate() method.
You also need to explicitly add the FirebaseDatabase import with the
rest of the imports near the top of your MainApplication.java file:
import com.google.firebase.database.FirebaseDatabase;
iOS
Add [FIRDatabase database].persistenceEnabled = YES; after the [FIRApp configure]; line inside your AppDelegate.m files
didFinishLaunchingWithOptions method.
Source: https://rnfirebase.io/docs/v3.2.x/core/default-app#Enable-Database-Persistence
Also, if you want to trigger any event when the user lost connection, you can use onDisconnect listener: https://rnfirebase.io/docs/v4.2.x/database/reference/OnDisconnect
Hope it helps
Related
I am trying to use firebase push notifications in my react native iOS app.
I am not sure about this step.
its said Add initialization code but when I search my react native app.
I saw appDeligate.h and appDeligate.m
idk which file to add and do I need to add objectC or swift code?
You have to add objective C code inside
AppDelegate.m file
Keep this thing in mind that must import firebase before FB_SONARKIT_ENABLED this line
#import <Firebase.h>
You just need to copy
[FIRApp configure];
And add it inside didFinishLaunchingWithOptions method
I installed firebase with react native expo using expo add firebase, then
I created a file (firebase.tsx) and added the Firebase configuration and initialized the app like in this screenshot with correct values].
I have added the google-services.json and
GoogleService-info.plist to the root of my expo project like the documentation say and i am importing firebase in my api service like this import Firebase from './../../../firebase'.
In my register function I call the firebase create user function
const response = await Firebase.auth().createUserWithEmailAndPassword(values.email.value, values.password.value)
The response is never returned, and if I use . then nothing happens either.
If I log firebase.auth I get an object with my API key and app name, etc. so Firebase is installed, but whenever I call the database for sign in or to access a collection nothing happens.
This is my package.json.
This is my app.json.
I added bundleIdentifier and googleServicesFile to iOS and package and googleServicesFile for Android.
currently Firebase seems not to be working in expo if you are using the android configuration. I have tried the web configuration and it has worked for me. Here is the youtube tutorial for it. Watch from 38:20 to set up.
I want to add Firebase Crashlytics to my React native project. I had a look online and some of the links suggested using Crashlytics with Fabric,
When I looked at Fabric and at somepoint it was mentioned that it was only available till 2020 and recommended to use Firebase Crashlytics. I am a bit confused.
Can any one suggest any link/guide to implement Firebase Crashlytics in my React native project.
Thanks
R
Its kind of simple.
First you need to add firebase to your project use the following link to link react-native-firebase .
https://rnfirebase.io/docs/v5.x.x/installation/initial-setup
Then use there doc to add firebase crash analytics into your code
https://rnfirebase.io/docs/v5.x.x/crashlytics/reference/crashlytics
for example:
import firebase from 'react-native-firebase'
componentDidMount(){
firebase.crashlytics().log('Test Message!');
firebase.crashlytics().recordError(37,"Test Error");
}
Add firebase to your project followed by crashlytics sdk. You might need to put the keys in your android and ios projects. Thts it.
Also have a look at sentry- its more suitable for JS based apps as it shows the line nos and the code which caused the crash. Crashlytics on the other hand as i remember shows only native crashes.
So I followed all the instructions, installed the library and linked it with react-native link react-native-firebase
Added the correct code in my AppDelegate.m with my GoogleService-Info.plist file added in my XCode project.
I also added -FIRAnalyticsDebugEnabled and -FIRDebugEnabled in the Arguments Passed On Launch like the documentation said.
Events like: user_engagement and first_open are showing correctly so I'm guessing that my config is OK, but my custom events aren't showing at all in the DebugView.
In React-Native-Firebase documentation it says
The default app is pre-initialized natively therefore there is no need to call initializeApp for the default app instance.
But in this example that I tried to follow, he uses initializeApp with for parameter debug: false so that events show up.
Code that I tried:
import RNFirebase from 'react-native-firebase';
const firebaseApp = RNFirebase.initializeApp({ debug: false });
firebaseApp.analytics().logEvent("test_event");
I did the same thing and it's not working at all. What is the correct syntax to send events on DebugView? I'm a little lost here. Thanks!
I'm using RN: 0.52.0
When using Firebase on ReactNative, it will show such error message:
can't find variable process
However, if I require firebase/lib/firebase-web.js manually, it will show:
can't find variable document
How can I resolve this?
I just went through the same issue while trying to use sockets.io in my react native app so hopefully I can help.
The reason that you cannot use firebase's node module is because there hasn't been a polyfill created yet for websockets support (which firebase is dependent on) in react native.
If you take a look at issue #619 in react native's repo you'll find the current discussion on creating a websockets api polyfill.
The way that we solved it is by using Jason's modified version of the sockets library and creating our own repo around just that file. Then we added the line below to our package.json dependencies.
"react-sockets": "crewapp/react-native-sockets-io"
The reason that Jason's version of the sockets.io client file works is because react-native is added as a user agent. You can find the code that makes this change at the top of the file:
window.navigator = {
userAgent: "react-native"
}
Once you've gone through these steps you should be able to require sockets.io / firebase as normal.
Just figuring it our. Pavan's answer is helpful, but it is not quite true when using with Firebase.
For firebase, please follow the steps:
Download the firebase-debug.js from wsExample. Or you can just install wsExample by npm and require the firebase-debug.js inside it.
Use badfortrains's forked React-Native:
"react-native": "git://github.com/badfortrains/react-native#WebSocket"
New the Firebase like this:
var firebase = require("../../firebase-debug.js");
var rootRef = new Firebase(Const.FB_ROOT);
Things should just work now!
I had issues with socket.io on React Native too, solution was to get notifications about new data and if data is big enough - get it by simple RESTfull request. in my case data was small enough to be sent all within notifications API.
I was using GCM service to send notification to phone from nodejs server. BTW, it uses less battery then socket connection and works great :)