Can't write on Realtime Database [duplicate] - java

I have created a Firebase, and I can write data into Authentication.
But, I could not write data into realtime database.
I have connected to Firebase
I have add Realtime Database
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:28.4.1')
// Declare the dependency for the Realtime Database library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-database:20.0.2'
I have set rules of read and write to ture
{
"rules": {
".read": true,
".write": true
}
}
I tried this to write data into Firebase but I get noting
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
// Write a message to the database
FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");
FirebaseDatabase.getInstance().getReference().child("aaa").child("bbb").setValue("ccc");

The code for writing looks fine, so I think the problem may be elsewhere.
If you created the Firebase console after you downloaded the google-services.json file, that file will not contain the correct URL, and the SDK may not be able to find your database on the server.
To solve that problem, you can either:
Download an updated google-services.json and use that in your app, or
Provide the database URL in your code with FirebaseDatabase database = FirebaseDatabase.getInstance("database URL here");

My code works this morning, but I did nothing except go to bed. As shown in the picture, the second step has changed. I clicked that Add the Realtime Database SDK to your app button thousands of times, and it was still a button. Surprisingly, it has changed to a checked state.
I did the google-services.json part as #FrankvanPuffelen said yesterday, but it didn't work.
I have restarted the Android Studio a lot of times and didn't work.
Maybe I should try close the computer lid next time.
But now I know that one should have the second step checked rather than keep it as a unchecked button.

Related

Multiple Firebase Projects in one app with different persistence setup

I want to setup 2 Firebase in 1 App, but with difference persistence.
One is enable, and the second is disable.
Please inform me how to set it up.
I take code from link below,
Multiple Firebase projects in one app
but it didn't explain,
how to setup persistence for the second Firebase.
I want to enable persistence for 1st Firebase.
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
And disable persistence for 2nd Firebase.
FirebaseDatabase.getInstance().setPersistenceEnabled(false);
As we see, that getInstance is static method,
how do we know that,
the returned FirebaseDatabase instance,
will belong to 1st or 2nd Firebase.
private void initSecondFirebaseAcct()
{
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("<your application id>")
.setApiKey("<your api key>")
.setDatabaseUrl("<your DB url that ends in 'firebaseio.com/' ")
.build();
try
{
FirebaseApp.initializeApp(this, options, "<database tag>");
}catch (Exception e){
Log.d("Firebase error", "App already exists");
}
mMySecondApp = FirebaseApp.getInstance("<database tag>");
mSecondDBRef = FirebaseDatabase.getInstance(mMySecondApp).getReference();
}
The way to make sure you are referencing the first or second Firebase app is to provide the specific FirebaseApp instance as a parameter to:
FireDatabase.getInstance(<app instance here>).setPersistenceEnabled().
If you use FirebaseDatabase.getInstance() you will receive the default instance, the first database app. So just provide the second instance as a parameter and you'll be good to go.

How to get the modified content from google sheets?

I am trying to get the modified content after the given time from google sheets. Nowhere I can found the api to get the data. What i can see is getting modified date alone from the drive Api. How can I get the data using Drive or Sheets Api? Give me the suggestions if Possible
Google Drive keeps a track of revision history of files that are contained on it. There is however, no way to obtain the revisions from a request alone.
Google allows for you to receive email notifications whenever a user makes an edit to your sheet, which you can set up by completing the following steps:
In the Spreadsheet's web view, click Tools -> Notification rules...
Under Notify me at myemail#address.ext when... select Any changes are made
Under Notify me with... select Email - right away
Click Save.
You should also be aware that you will not get a notification for edits made to the sheet by you - notifications are only received when another user edits the sheet. Whenever you get an email notification, you will receive a link to view the changes to the spreadsheet in the form of a read-only web view link.
You can work around this programatically, though there isn't one right way and it can be quite complicated. You can use the Revisions: list method of the Drive REST API to get the information about the user that made an edit, as well as a list of links which you can use to export that revision of the sheet to another MIME Type, as shown below in the request response.
Requesting:
GET https://www.googleapis.com/drive/v3/files/SPREADSHEET_ID/revisions
with revisions/exportLinks,revisions/lastModifyingUser/emailAddress as the fields field and replacing SPREADSHEET_ID with the ID of the spreadsheet will give you a 200 response:
{
"revisions": [
{
"lastModifyingUser": {
"emailAddress": "username#domain.ext"
},
"exportLinks": {
"application/x-vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=ods",
"text/tab-separated-values": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=tsv",
"application/pdf": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=pdf",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=xlsx",
"text/csv": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=csv",
"application/zip": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=revisionNumber&exportFormat=zip",
"application/vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=SPREADSHEET_ID&revision=1&exportFormat=ods"
}
}
]
}
With the links to individual changes, you can fetch and compare the different versions of the Sheet using Apps Script, and output A1 notation of the cells that have different values between versions. This, with the email address from the original Revisions: list request, is enough to compile a file or a log containing.
You can put this into a simple onEdit() trigger bound to the sheet will allow you to automatically get the changes each time a user edits the sheet.

An unknown error occured please check HTTP result code and inner exception for server response

This picture is of the upload fragment made by me using android studio and when the list item is been clicked the object should be downloaded.It's Working fine and the object is been downloaded on my phone and the other phone on which I installed it. But when I shared this application to others it gives the error marked in the picture on their phones.Updating the play services is not removing the error.
After 2 days of frustration, I found out that there is a bug in firebase storage.Thus, you've to simply turn off and on your storage manually,thanks.
May be this is the wrong path bug but in my case resolve this here is the example
i use this code then i received error
public static StorageReference getChildProfileStorage(String vid){
StorageReference storageReference= FirebaseStorage.getInstance().getReference();
storageReference.child("ParentDataStore").child(CurrentUser.getInstance().getEmail())
.child("ChildDataStore").child(vid);
return storageReference;
}
here is the line that solve my problem
public static StorageReference getStudentProfileStorage(String vid){
StorageReference storageReference= FirebaseStorage.getInstance().getReference("ParentDataStore")
.child("StudentDataStore").child(vid).child("profile");
return storageReference;
}
Sorry, I am late. but today I am also facing the same issue. What I've done is to update the storage library in build.Gradle (app) file. and it solved my problem.
Basically when you configure storage using Android studio firebase assistant. This will import the older version of the app. So don't to update a newer version
For me, I was providing invalid system file path while calling getFile() of the FileDownloadTask, I was initially trying to download a file from Firebase.
Here the issued is raised as the downloadDirectory was invalid
File localFile = ...;
FileDownloadTask downloadTask = FirebaseStorage.getInstance()
.getReference()
.child("fileName")
.getFile(localFile);
Here the path of the localFile I was providing was invalid.
After 3-4 hrs depression,i got the actual reason.
While downloading,I need to give storage permission of writing but I only gave read permission. In lower androids , read-write permission is same.Basically I was used to/accustomed to these lower version devices that's why forget to add write storage permission for the upper androids
I had the same issue and when I checked the Storage rules:
allow read, write: if false
I saw it was.
It worked when I changed it to:
allow read, write: if true
I've just updated the implementation in the build.gradle(:app) to
implementation 'com.google.firebase:firebase-storage:20.0.1'

How to know in firestore when a collection have changes?

I have a collection who contains items of a restaurant menu, i want to know when a new item is added or have a change(price, name, description), because i want to notify this to the app and download this changes in a internal database.
I was trying to create a firebase function to modify a field called version and this way campare version in the app vs firebase version of the collection but i really dont know how to work with firebase functions.
can someone give a recomendation?
Cloud Firestore is a flexible, scalable database which keeps your data in sync across client apps through realtime listeners and offers offline support for mobile and web applications.
So in order to know if something has been changed in your database, you need to attach a listner on a particular location. Assuming you have a collection named cities and a document named SF, plase use the following code. It's a straight forward example.
DocumentReference docRef = db.collection("cities").document("SF");
docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot snapshot, #Nullable FirebaseFirestoreException e) {
// see which fields changed
}
});
Unlike Firebase Realtime database, Cloud Firestore has data persistence enabled by default. So there is no need to keep your data also in a internal database.

FirebaseUI with Firestore Recycler Options and custom adapter

I'm using the FirebaseUI and a custom adapter to show data coming from Firestore database and storage, inside a list of Cards (on Samsung Galaxy S6 and S8 devices).
I was first using the method described here (from the firebase.google webiste) but I then switched to this one (from firebase github) as it is more suitable for my needs.
When I tried to see if data were coming from the server or the cache with the first method, the code below worked as expected:
String source = snapshot != null && snapshot.getMetadata().hasPendingWrites()
? "Local" : "Server";
However, when using the second method, when I get my list of Snapshots, inside the adapter, from the query, and try to see if the data are coming from the server or the cache, I always get:
mSnapshots.getSnapshot(position).getMetadata().hasPendingWrites()
equal to false
and
mSnapshots.getSnapshot(position).getMetadata().isFromCache()
equal to true
independently of my internet connection.
Why? And how can I see if the data are coming from the server or the cache using the second method?
Thanks

Categories