Firestore chat app structure - Unread / read message schema - firebase

I'm building an app with a social element in that users can have 1 to 1 conversations. At the moment a user can request a conversation with another user and upon acceptance, a cloud function is triggered which then assigns that conversation to both users. This works really well up to now and I have users being able to send each other messages instantly
My problem lies in that I want users to be notified of messages that haven't been read. I know that I could implement notifications via FCM but I also want the user to be able to see that a conversation has unread messages in their conversation list
Is this possible with firebase?
Edit: I should rephrase, I know this is possible. I just can't think of any structure that would be efficient. I could pass a messageRead Boolean property on each message, however I would need to update this property everytime a message is read by updating the field immediately after getting the documents.

Related

How to handle FCM subscribeToTopic and unsubscribeFromTopic with Flutter

I am developing an app for which I use firebase as the backend. I am using FCM to send notifications to my users, however, I am not yet grasping how to use subscribeToTopic and unsubscribeFromTopic.
My use case (which I do not know how to get it working):
After a user installs the app, he will be subscribed to the main topic of the app (I have this working). The user could then go to settings to unsubscribe from the main topic (I do not have this working).
The struggle:
All the different tutorials I find describe how to subscribe to a topic, they call subscribeToTopic('some topic') in initstate of the welcome screen. Is it required to do this everytime the app starts? What happens when the user calls unsubscribeFromTopic('some topic') and the next time they start the app subscribeToTopic('some topic') is called again in initstate?
My idea: first time the app is loaded I call subscribeToTopic('some topic') and never again. Then in the settings screen a user can unsubscribe (and subscribe). Does this work? What should I pay attention to?
Thanks in advance for helping!
You check if the app is launched for the first time by either using shared_preferences which stores this information locally or by storing a variable on Firestore that tells you if the user has logged in to the app before.
You subscribe to the topic if it's the first time and if it's not, you do not subscribe.
The first time you open the app show a welcome screen or something like that, where you ask the user to receive notifications or not (which is always more user friendly).
If the user continues you save this value in a local database like shared preferences or hive.
If the user subscribed you call the subscribe to topic method
If the user does not subscribe ofcourse you do nothing.
Then on the settingspage:
Show a switch which value is gathered from the local database you already defined.
When the user taps the value is stored, based on this value you subscribe or onsubscribe.
This is how I have done it in my app where I also use topics instead of tokens.

Swift - FireStore/FCM (Firebase Cloud Messaging)

I've incorporated Firebase Cloud Messaging into my app. After messing around, I kind of understood the premise of how it operates. So, as a result, I structured my code so that when users sign up the FCM token is stored. After creating two accounts, I realise both FCM tokens for the user were the same.
Looked online and sorted this issue, and now I can refresh the token on launch, and still append the new FCM token when users initially sign up.
So now, I ask the question - Users are created with a fcmToken field (which I can refer to in my code), however, due to the fact a new token is generated on launch each time does this render the token(s) stored for each user useless? Or can I still push to the specific user using the fcmToken?
I've looked online, but can't seem to find an answer.
FCM tokens don't uniquely identify an individual end user. They identify a specific installation of an app on a specific device. When sending with that token, it doesn't matter who is signed in to the app (or if anyone is signed in at all) - the app will still receive it and need to figure out what to do with it. It's up to you to decide what to do with that message, given the sign-in state of the user. If you expect that your app could have multiple users sharing a single app on a single device, then you will probably want to send something in the payload to determine who that message was intended for, if necessary.
#doug great answer, but there's a common implementation problem when people share a device, so please add a warning, something like: often only the last logged in user should receive push notifications, otherwise he could see messages from the person who was logged in before. your backend should not only store all the devices a user is logged in, but also for each device who the last active user is and check this upon sending a push. the question whether you receive push or not when logged out is a common topic, too.

Sending message to custom user using firebase

Id like to make a chat between two custom users.
I need to send an message to custom user.
scenario:
User1 get User2 ID from my server
My server get User2 ID from Firebase (probably using Firebase admin)
My server return User2 ID to User1
User1 send message to User2 via firebase
Is it possible to make? Do I have to use Firebase Admin + FCM or something else?
[Giving you a simple guideline and logic here]
One of the simplest ways to implement it is by using Cloud Firestore.
You need to create 2 tables, one for the users and one for the chat. The users will be used to store the users' info including UID, emails, friends (array, to store a list of friends), chats (array, to store a list of chats), etc. While the chat will be used to store all the chats. (You can design your own database, some guideline can be found here)
When the user signed-in with Firebase Auth, you can query the database to get the details from the Firestore. Assuming you put everything in some UI element (recyclerview), once you click on the user's friend (friends are stored in the Cloud Firestore mentioned above), a new chat document will be created in the chat database/table, and under the other user (User 2) document record in the users table/database, update the chat array with the new document ID. For the User 2 UI interface, new chat will be created/updated as Firebase has something like onChangeListener() to update your client.
You will need to design your UI for the chat allowing the user to send messages. The UI will need to fetch all the chat records from the chat document. When any party sends a new message, update the message to the chat document.
So basically that's the rough idea, you can get all those references from Firebase Doc.
For notification, you can use Firebase Cloud Messaging to notify the user!
The actual function of FCM as stated in the doc:
Using FCM, you can notify a client app that new email or other data is
available to sync. You can send notification messages to drive user
re-engagement and retention. For use cases such as instant messaging,
a message can transfer a payload of up to 4KB to a client app.
Therefore, it is not intended user to user but is for the App admin to the user for certain events.
For Admin, it is not encouraged to call Admin all the time for a production consumer app.
P/s: I don't spoon-feed with code but to encourage people to learn from their own research.
Hope it helps!

Send Firebase Cloud Messaging notification to users by user property

I'm trying to send out an FCM message to a specific set of users (or really, a single user) based on a specific user property but looking through the FCM HTTP API I can't seem to find a way to do that. I can send to users via topics, registration tokens, and device group notification keys, but I don't have any of that infrastructure set up in the near term. I know this functionality exists as you can send such a message via the UI, but I'm not seeing how to do it in the API documentation as yet.
There is currently no parameter that you could use to specify a user property (or even for user segments) that will serve as a target for the FCM API to send the message to.
As you've already searched, the only targets possible are single/multiple registration tokens (to and registration_ids), topics, conditions, and device groups (notification_key).
The option you're looking for is currently only available when using the Notifications Console.

How to move old chat data from Firebase database to another database

We are developing a chat app for Android and iOS which uses a Firebase database for storing chat messages. We don't want keep all chat messages in Firebase database because storage gets bigger and expensive everyday. So we want to move 2 days old chat data to another database. Our purpose is to load new messages from Firebase storage and to load 2 days and older messages from another database when user wants to see older messages by scrolling up or pressing "load more" button.
Our database is like this:
Chat Room KEY
Message Id
Message
Time and Date
Reciever Id
Sender Id
I have a few questions.
Can we automatically move older messages to another database based on message created date to decrease database storage costs? What is the best method for this? Do we need to use Firebase Functions?
Do we need to implement auth for our new database so that it know if our user wants to see data? Or is is it possible to query old message data from client side without authorizing our app users. Will it have security problems?
Which database we should use? How would you solve this problem?
Thank you.

Resources