Show local notification with Stream Builder - android

I have made a chat app with Flutter and Cloud Firestore the app is working but I need to show local notifications when a user gets a new message how can I do that.
I have used StreamBuilder to display the messages in the chat screen.
Please help me.
Thanks in advance.

It depends on the data you are recieving. A stream builder may rebuild on multiple occasions including on network state change. so you can't add it directly to the build method. For example if its returning a list of items, then you can create a variable and update this variable with the length of the response. for example if the length is 5 in the first response. Update the variable to 5 and on next instance from stream you may get a 6 then check if the existing value is lesser than the current length. Then perform your custom action here (print or notification) and update the length.

Related

React Native and Firebase: how to limit number of times data are fetched

I'm currently working on an app with React Native using Firebase as backend. I won't be too specific on the details, but, basically, the app has to constantly fetch for new data uploaded, so I added this part in my main code:
useEffect(() => {
db.ref('rooms').on('value', (data) => {
if (startRoom.id) {
ping = (data.val());
setRoom(() => (data.val())[startRoom.id]);
}
});
},[ping]);
This way, every time new data are added in the object "rooms" in my db, the app is able to update all variables linked to it. This process however can cause some huge slowdowns and performance issues: indeed, when there are a lot of data uploaded by different users, or when data are uploaded very quickly (let's say, uploads are handled by user clicks, so very quick clicks can cause the db to go nuts), the app is constantly refreshing the page even several times per second, causing the slowdowns I was talking about.
So my idea is: is there anyway I can limit the number of fetches the firebase ref method does over time? Like, if the database is uploaded seven times in a second, it only catches the last one, ignoring the uploads which, for example, happened in a very short time. In other words I wonder if there is a way to limit the "sensitivity" of the ref method or the useEffect method of React Native.
Thank you very much!
If updates are happening multiple times a second, I think you should change your approach.
I think you should instead of updating your app with the data the moment it changes, store the changes in to an array changes, and set one interval that run for every, says 3 seconds, check the changes array. If there is data in it, use it to update your app and then clear the array. This way you can make sure your app only update at a fixed rate even if there is a lot of changes
what's the data type of ping? If it's an object, your useEffect will run every time because of the way JavaScript checks for equality on objects (they check reference I believe).
A way around this is to use useDeepCompareEffect or lodash's isEqual. Or just use a primitive value as the dependency for your useEffect.

Having problems applying android code architecture/design patterns

I want to achieve the following:
I receive a notification via FCM with a specific id in the data properties. I get the id both when my app is in foreground and in the background/closed...so far so good.
Now I want to do the following:
Make a volley request to e.g. /books/id to get the corresponding book informations.
Save those informations persistently in a Room database
Display them in a RecyclerView
Scenario 1: App with RecyclerView is in foreground:
As soon as I receive a new id via the FCM notification data, I want my RecyclerView to append another list item with the newly fetched book informations.
Scenario 2: App is in background - notification gets clicked:
When the notification gets clicked, the MainActivity with the RecyclerView gets loaded, showing the newly fetched book informations.
Scenario 3: App is in background - notification gets swiped away:
After some time the notification was swiped away the user re-opens or restarts the app. The MainActivity with the RecyclerView and the newly fetched book informations is shown.
My problem:
I don't quite know how to structure my code to accomplish this.
I tried making the volley request inside of FCM's onMessageReceived() but that resulted in an error and I don't know where and how I could do that otherwise. I tested the volley request in another project and it worked there.
Currently I try to learn how to work with the Room database.
I would really appreciate if someone can give me an insight of how to accomplish something like that. I really need to get this to work soon.
Thanks in advance! :)
EDIT 1:
I dont't get the error inside of the onMessageReceived anymore. I receive the correct response with the given book id.
Now I want to save these informations into the Room database and reflect the change in my RecyclerView. I think I'm going to use androids architecture components like here:
android architecture components example
I think that's exactly what I need. However, I still don't quite understand how to deal with it and structure my code accordingly.
Scenario 1: You may save it in DB, or just do broadcast and save it for instance in Activity then display a list.
Scenario 2: You should keep all display data in DB. After click on notification when RecyclerView is in background, you should handle the intent that pass through android.intent.category.LAUNCHER of intent-filter and put some info there to make a request and update the list.
Scenario 3: Finally, just do a request, save in DB, then display all in the list. This is a good approche, it takes extra time, but you always keep it updated.
Also read about efficient hot swap data with DiffUtils

Keep track of item in recylerview?

I am making a chat application in which when the user hits send button , the message is added to the recycler view and at the same time sent to the server.How can i keep track of the message item in recycler view so that if the message is not successfully sent to the server (due to network problem ),i can show a resend option next to that message.
following is the flow
sendMessageToServer(message);
messageItems.add(message);
notifyItemRangeInserted
The easiest solution -but not the most correct one- would be as the following:
add a boolean attribute isSuccessful to the message class.
when you send the message and the get the response back from the server change the corresponding message. and call notifyDataSetChanged on the Adapter.
and I recommend this take where they build this open source messing app to get a better understanding how to design you Application Architecture, as my solution is not the best but I guess is the easiest.

How can I update certain values in recyclerview whenever onMessage function called?

I am making an android app and facing this similar issue I am sending data my SQL server using FCM which received in onMessage function in my app, but at the time of receiving the data I am not sure weather that specific screen exist or not? maybe user was on another screen at that time.
And along with this there will be number of different data objects for different screens? How can I make sure that data will properly update the existing data I know how to update data so please don't think about that, problem is how to call update functions of different screen which may or may not exist at that time.
I have spent more than 3 days on this solution but can't figure out. Please any good suggestion could prove a lot of help.

Android - lazy loading of ListView with web-SQL results using JSON and http?

I'm currently working on an app which has to query a web SQL db, and show the results in a ListView, and I would really appreciate some input as to what is the best way to do that.
I would like the results to be shown as quickly as possible, so if I can somehow show the first result immediately while still loading the rest that would be great.
Reading on the subject, it seems the best way to send the data (which includes a small image) is using a JSON object (or array).
The ideas I had so far:
* Http requests with index of last result - downside is that the server will run the same query over and over again and just send me a few results at a time.
* Open a socket between device and server until user leaves the results view - downside is excessive use of network resource.
Do they sound OK?
Is there something else I'm missing?
Thanks!
The ideas I had so far: * Http requests with index of last result -
downside is that the server will run the same query over and over
again and just send me a few results at a time. *
=> This is the standard idea but I would say sending request and fetching data depends on the particular requirement, like you should use Service concept if you would want to show first set of result and at the same make a call for another sets in background.
So whenever service gets another sets of data, it sends a message to your activity with the received new data set and your activity will update the UI with the available new data.

Categories

Resources