I created real time database on firebase, from one android app im sending data and in other app(second separate app different from the app im using for send data, for just retriving if there is change in data) i want to receive that data, how can i do this, because if I'm trying to access like this in onCreate
FirebaseApp.initializeApp(this);
DatabaseReference data = FirebaseDatabase.getInstance()
.getReference("https://my-project.firebaseio.com/nodeName");
I'm getting first initialzeApp also i tried with adding google-service.json from my app that sending data, its saying that its different from project name, how can i do that the firebase structure is like this
{ https://my-project.xxxx somenumbers
{
node{
lat : "value"
lng : "value"
}
}
}
For every Android app that you create in Android Studio that you want to use with a specific Firebase project, you should create an Android app in the Firebase console too. You can do this by going to the overview page of your project and clicking "Add another app".
In Firebase console there is option to add multiple apps in the same project. All applications that belong to same project can access same Firebase database. Each app has its own .json file connected with same project. So, if you want to access database from any other app then you need to connect that app with the same Firebase project at the console. And to get the values, make a reference to node and apply value change listener to it. Official documentation will help you
Your code should be like this
DatabaseReference otherDB_data = FirebaseDatabase
.getInstance("https://my-project.firebaseio.com/nodeName")
.getReference();
instead of
DatabaseReference otherDB_data = FirebaseDatabase.getInstance()
.getReference("https://my-project.firebaseio.com/nodeName");
and Don't forget to authenticate in your app to get realtime database access
Note: Authenticating is useless if Realtime Database Rules are like
{
"rules": {
".read": "auth == null",
".write": "auth == null"
}
}
so everyone already have the permission
You can do this as many times as you like, but you can only have one
connection to any given database instance at once.
Source : Firebase Documentation - Connect your app to multiple database instances
Related
I am working on an application where i have implemented the chats in my application. So i want to know that how can i delete a whole chat like suppose I have sent a message to someone or someone sent a message to me so how can i delete that particular chat
this is the image
enter image description here
in the image above are tha chats with whom i have interacted. so if i want to delete any chat out of these how can i do that??
this is the my firebase data
To delete a node from the database you call removeValue() on a DatabaseReference to that node.
So you can create the entire Chats node with:
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
root.child("Chats").removeValue();
To delete a lower-level node, you can specify the entire path to that node:
root.child("Chats/hI151.../messages").removeValue();
You will need to know/specify the entire hI151... value in this code.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Hi I am working on a chat app in Java on Android Studio and trying to read data from firebase Realtime database which is structured like so
{
"Chats":{
"<chatID>":{
"<title>",
"<lastmessage>"
"<timestamp>"
"Users":{
"<user1ID>",
"<user2ID>"
}
}
},
"Users":{
"<userID>":{
"<name>",
"<email>",
"Contacts":{
"<contactID>",
},
"Chats":{
"<chatID>"
}
}
}
}
Currently I have 3 separate event listeners that add read data into a 2D array
ArrayList<ArrayList<String>> chatsDetails; //[("chatID","timestamp",userUID","userName","userEmail"),...]
The code is quite long but the titles of the event listeners (shortest code necessary to reproduce the problem) are like so
/*Listener 1- populate ArrayList with chat ID from Firebase database*/
databaseReference.child("users").child(mAuth.getCurrentUser().getUid()).child("chats").addValueEventListener(new ValueEventListener() {
});
/*Listener 2- populate 2d ArrayList with users uid and timestamp from Firebase database*/
databaseReference.child("chats").addValueEventListener(new ValueEventListener() {
//requires the chat id from listener 1
});
/*Listener 2- whenever values under users changes, update user email and name in 2D array*/
databaseReference.child("users").addValueEventListener(new ValueEventListener() {
//requires the user uid from listener 2
});
Specific problem:
The issue is when I do a Logd to the console, I realise that the listeners do not run in the order I require them to. This results in the name and email of the users not being retrieved and stored into the ArrayList.
Desired Behavior:
I want to know if there is any way to make the event listener go one after the other (So the log console should display Listener 1,2,3 executed in that order
I have tried to look for OnCompleteListeners but couldn't find any for this, and I tried nesting addValueEventListeners which didn't work. The issue is that when a new is added to the database it triggers all 3 listeners at the same time, so I'm not sure how to make them work in order. Any guidance would be appreciated, thankyou
.addValueEventListener()
when the database updates it does something that triggers this function to run .So lets say you have a copy of the Firebase data on the device
and everything is sync correctly then NOTHING happens (you dont iven use the internet connection to get data u just ask the db if it did any modifications or no) ,Now all the data you get from you db is from your local db version of your Firebase db.
this is just details aboute how firebase works systhematicaly.
If data gets inserted you get the modifications that happened only + the data that didn't got changed in your "local Firebase db" .
So now you need to store data in a List by clearing and refilling it when a modification happens on "remote Firebase db".
And then work on the Lists you have on your device .
I have an application at the Google Play and there are already a few hundred of users.
Now, I'm working on an update that will allow users to share user-to-user content via Firebase Dynamic Links.
In order to do so, I created a subdomain and all the required things and I added this code:
FirebaseDynamicLinks.getInstance()
.getDynamicLink( getIntent() )
.addOnSuccessListener( this, pendingDynamicLinkData -> {
Uri deepLink = null;
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.getLink();
String bookID = deepLink.getQueryParameter( "id" );
if (bookID != null) {
startBookDetailActivity( bookID );
}
}
} )
.addOnFailureListener( this, e -> {
}
);
However, the users who already have the app installed on their phone don't have this piece of code in their application which means that if they click on the dynamic link it won't do the job.
Is there any other way I can handle the dynamic link for the period until all users get the latest version?
Thank you
You could have the user copy and past the code into the app but ultimately, this also requires that the app is updated to do so.
Perhaps you could use a cloud function that would return the data in the browser rather than the app, you could do this with a button that appears after a second and manually invoke the call.
But it might just be easier to encourage users to update and implement a callback system using real-time or firestore as a user inbox system for this kind of edge case.
I have a problem and I need to know how does the firebase config gets the region/country for a user, is the region based on google account, or what ? also how does it get the region without any permission ?
I have met the problem when I try to get the device region. Here are many ways to get this region, you can use below ways:
use "ro.carrier" get the cid;
use Locale.getDefault().getCountry() get the short country name;
use (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE) to get the region of SIM card.
you may use all of them and given priority.
For Firebase, I have met a short code below, maybe this can help you:
String feature_url = "feature_config";
Log.d(TAG, "init, feature_url: " + feature_url);
mRootReference = FirebaseDatabase.getInstance().getReference();
mRootReference.child(feature_url).child(BuildConfiguration.DEBUG ? "debug" : "release")
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange");
FeatureConfig.getInstance().updateConfig(dataSnapshot);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled");
}
});
this ie a method used to get the DataSnapShot and then get the region information from it.
Since I dont know whether this could help you, I dont past to much code here.
Hope it helps.
This may give you some idea!!!
You have your own FCM key is strictly required for native implementation.
While setup to get FCM key user should provide region/country
How to set up your own FCM keys:
1) Log in with your Google Account
Open https://console.firebase.google.com/ and log in using your Google account (if the page asks you to do so).
2) Click on Create New Project
In the next box that appears, click on Create New project.
3) Compose the Project Name and select your country/region
You’ll now be prompted with a modal box like the one shown below. Under Project name, write the name you want and below that, select your Country/region. Once you’ve done this, click on the Create Project.
For detail go through this
Android platform is really fun to work with especially when it comes to resolving issues. Indeed, there is possibly everything there is to know about Android development on the internet.
Alright, I've been searching for about a week and haven't found anything that was close from working. Let's dive into it.
We are building an Android Application that requires a read/write access to existing Contacts on a device. It has become really easy to read a contact's set photo using this method :
// Returns a stream reading a contact thumbnail
public InputStream getContactThumbnail(int id) {
// Stream reading contact image
InputStream stream = ContactsContract.Contacts.openContactPhotoInputStream(
context.getContentResolver(),
ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, id
)
);
// If image is null, try to read Facebook image
if (stream == null) {
stream = new ByteArrayInputStream(getFacebookPhoto(id));
}
return stream;
}
Now, the previous method receives an id as a parameter and returns a Stream making it possible to read the contact's thumbnail. It is needed to be a stream because the phone acts like a web server and has multiple threads running. If a thumbnail is requested several times in the same short time lapse, an OutOfMemoryException will be thrown for sure.
I need to correctly implement the getFacebookPhoto(int) method so that it returns whatever stream reading the Facebook profile picture of a contact that has his or her contact linked with his or her respective Facebook profile. I've tried and failed so many times.
Hypothesis #1
If a contact is linked with a Facebook profile, it has to have the Facebook ID saved somewhere. If this information is accessible, it would make it easy to get a Facebook profile picture using graph. Problem is an internet connection is needed to do so.
Hypothesis #2
Facebook thumbnails are saved somewhere on the SD card. Maybe there's a link between a Contact and those files that can be found through an SQLite request?
Hypothesis #3
sigh, I look desperate. Okay, if I understood correctly, a phone Contact and a Facebook Contact are not the same things in the database. If you query all the contacts from the following URI :
*ContactsContract.Contacts.CONTENT_URI*
you only get contacts that you created and nothing regarding Facebook links. Is there a way to find all linked contacts and get their respective photos?
Conclusion
Yeah, that's about is. To sum it up, I need to read all contacts information. For each contact, I have to find its photo. If the user has not set a picture to a contact that is linked to a Facebook profile, the profile picture which it was linked too must be read.
Up until now, the StackOverflow community has been of a great help and saved my life and job countless times. It is possible, I've seen it in other apps.
Thank you for spending of your time, it is truly appreciated.
EDIT
Let's not give up! I will start a 100pts bounty as soon as I can.
Your hypothesises are right to a certain extend. In contact application this things are handled as folows -
Device's Contact & its Facebook account are mapped by Contact's _ID & Facebook's id, this is one-to-one mapping. So from this mapping first you have to find out the Facebook id
of the conserned contact. But in which table this info is stored & wheather that table is eposed to you or not, URL to that table completely dependent on vendors.
From Facebook Id we can get corresponding profile image either from Facebook's server or from Media DB, if it is also cached in.
But this is not supported by all OEMs. And implementation varies from OEM to OEM as Google don't enforce for any common standard implementation of it.
So there is no garantee that a single implementation will work for all devices from different OEMs.
You should really go and play around with Graph Explorer on the Facebook Developers site it will help you a lot in figuring out what you need to do to get certain things. In order to get the picture for an person you just have to do is do a simple GET HTTPRequest with the graph path of /ID?fields=picture (where ID is the facebook id of the contact) which will return a JSON object that contains the link to that person's profile picture. From there it should be fairly simple for you to get the image.
You can also do the same thing to get all of a person's friends with the picture information by sending a GET request to /me/friends?fields=picture. It seems like you are trying to avoid a web connection but if the android contacts do not store the facebook id then you will have to get the ids yourself I'm afraid.
Hope that helps.
Why don't you use FQL, for Android you can use -
String query = "SELECT uid, name, pic, pic_small, pic_big FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", query);
mAsyncFacebookRunner.request(null, params, new CustomRequestListener());
where CustomRequestListener() extends RequestListener in the Facebook Android SDK.