Creating groups with Firebase Realtime Database in Android app - android

I have an Android app that uses the Firebase Realtime Database. At the moment it is a single user app, in that when a user logs in, using Firebase Auth, the data they create is kept private to them. What I need to do for the next phase of the project is to allow for the creation of teams or user-groups that have access (read and write) to the data currently created by a single user.
The database structure and rules for doing this are understood, however, the issue I have is how to create the groups.
I did find the following blog and it is very close to what I need, it does not make clear how a new user would create the pending membership entry in a specific "chat" (this is a group chat sample).
https://firebase.googleblog.com/2016/10/group-security-in-firebase-database.html
Any points on resolving this final issue would be much appreciated.
Sid

Just simply create groups like below
|--Groups
|---group1
|-----uID1 - owner
|-----uID2 - participant
|-----uID3 - participant
..............
follow this https://firebase.googleblog.com/2016/10/group-security-in-firebase-database.html link. There are example groups and rules. It will help for u to proceeds it.
Follow this https://firebase.google.com/docs/database/ios/structure-data to make good structure of data to increase performance

Related

Hiding/making unaccessible features in android application according to user type

So I am creating an Android application in which suppose there are 2 user types, userType1 & userType2. Now what I want is that only userType1 will see and be able to access the post button, and userType2 Can't see and or access it. So what is the best practice for that?
The best practice would be to implement Firebase Authentication and save user data, either in Cloud Firestore or in the Realtime Database together with the user type. When you start the app, first you have to read the user type and then display the UI elements according to it. I have answered a question in which I have explained how to redirect the user according to its type to the corresponding activity. Here is how you can do that using Cloud Firestore:
How to redirect a user to a specific activity in Cloud Firestore?
Or using the Firebase Realtime Database:
How to redirect multiple types of users to their respective Activities?
The same mechanism can be used to hide/display a button. Don't also forget to secure your data using security rules.

group chat in flutter using firebase with a twist

Disclaimer: I have never worked with firebase or any other DB so my idea is pretty rough and I will be glad if u correct me in some of my assumptions. Also this is my first question on StackOverflow so I hope it will be detailed enough.
I am currently working on my project which is basically a group chat in flutter using firebase, where I should be able to create a new group chat(create a new DB in firestore) trough the app, let other ppl join(assign them to the new group chat DB).
The twist: If I send a message to the group chat I want other ppl to see a pop-up saying: "do you accept this message", no=> doesn't show / yes=> Shows the message but deletes it for others.
This app is nothing but my idea of how to confirm my theory and also learn a little bit more about both flutter and firebase, so please do not mind if it's useful or not.
Assumption: Each groupchat has it's own DB (Can I create a DB in firestore trough app / send request to?)
Question: Can I assign a specific ID to each message and choose and further edit who is going to be able to see the message trough the app(app_instance_1"I accepted the message so only I and sender can see it")?
Here's a great article for making a Flutter chat app: https://medium.com/flutter-community/building-chat-app-in-flutter-with-firebase-888b6222fe20. If you need more detail or help just write a comment.
About your assumption. Each app usually is connected to one database, and within the database, you can have separate documents to hold each group chat. You can also set up documents for each individual user to save the chats that only they can see. Within documents, you add collections that hold your data.

How firebase real time database reads data?

I am working on a chat app where messages store in firebase realtime database. Now if i make a node shown below ( chats-Between-A-and-B-Id are autogenerated chatroom keys ), so i want to know that when user S open chats with user T in chat app so Database will read only the messages stored in chats-Between-S-and-T-Id and will not read others chatroom messages !? am i right ? if yes then will it reduce the pricing?
chats-
chats-Between-A-and-B-Id-
messageID1-…
messageID2-…
messageID3-…
...
chats-Between-o-and-p-Id-…
chats-Between-s-and-t-Id-…
chats-Between-x-and-y-Id-…
OR
If i store data like shown below then if user S open chats of user T in chat app so Database will read whole messages and
cahts-
messageID1-…
messageID2-…
messageID3-…
...
sort them like (shown below) so it will read all messages and sort them and show in app to the user !? which will increase the pricing ?
if(chat.getReceiver() == senderID && chat.getSender() == receiverId ||
chat.getReceiver() == receiverId && chat.getSender() == senderID)
{
(mChatList as ArrayList<Chat>).add(chat) //show chats
}
I want to know which approach can save my money.
Edit
I am using the 2nd approach (i got it from a chat app tutorial). Where we sort messages according to senders and receiver but after reading the answer of #sharath i get that if i go with this it will read every message from chat node and then sort them according to sender-reciver and if there are 1 million messages from all the users then it will read 1 million messages and sort only some of them only for 2 users! It will make me poor. So i am thinking about using the 1st approach where i will get messages from chat>>roomIdBetween2Users so it will ont read others chat and give me secure, maintainable and affordable databse.
And this is the image of my database here chats is parent node contains all the messages of all the users and sort them as i mentioned in the code for 2 users. There are already approximately 500+ messages in chats. And it will be so dangerous in future.
Its a little tricky situation to answer this without knowing your firebase DB structure. Am making an assumption and generalising this answer.
Firebase is a No SQL Database which stores data in tree structure or Parent child manner
Main important logic you should think of while developing anything with NoSQL databases are you need to create the Firebase DatabaseReference to the deepest node possible. Firebase queries and reads data which comes under the referenced node.
For example : if you have firebase structure as below
See this image of Firebase sample structure
If you want to access chats of User S, Create Reference to ID_User_S node.
note: If you create reference top node "Chats" then snapshot returns all child information under it
Regarding pricing : Yes the lesser the data you read lesser the price you pay. If you are referencing to deepest node possible you read lesser data. Try to optimise all firebase queries to reach to deepest node possible for creating a reference.
Now coming to your structure
chats-
chats-Between-A-and-B-Id-
messageID1-…
messageID2-…
messageID3-…
...
chats-Between-o-and-p-Id-…
chats-Between-s-and-t-Id-…
chats-Between-x-and-y-Id-…
If you create a database reference to Chats node then each one trying to access the chat will read every child under it which includes others chats as well.
So for efficient usage create reference to chats-Between-A-and-B-Id- so that you can get exact list of messages under those 2 users

Get data entry count using firebase analytics

I am new to Firebase Analytics and I went through some online tutorials to learn about it. I have a simple 'Notes' app (Android) in market, which allow users to create and save notes offline on their device.
It would be great, if I could get a total number of notes that have been created till date with my app. I know, I can add an event which will notify Firebase when user creates a new note, but what about the existing notes that are present in the database?
Is there any way to get such statistics through Firebase Analytics?
UPDATE:
I have included a user property as notes_count, but I am not able to find a way to see the total. How to do it?
You could set a user property with the number of notes the user has created instead of or in addition to logging the event. You are limited to 25 different user properties total though, so use them with care.

Use single database for multiuser in Android app?

I am developing a app and want to see offline data if Internet is not available . And this app can use multiple user like as after logout one user then another user can login and can see own data So how we can make database to accessible for among user
Use a discrimator column in your database table design to mark owner ship of every record.
Then you can impl. your data access methods so that they only query records for active user.
It's a design issue not a developing problem ;)

Categories

Resources