I want to implement something similar to the Stories feature in Facebook or WhatsApp.
Possible approaches :
At first, I wanted to open a web socket so I could receive the latest Stories updates (new story from a friend, or if a story was removed).
There'a another alternative - getting those updates using FCM pushes.
A service which receives the pushes broadcasts the "new story"/"story removed" broadcast using a LocalBroadcastManager, and the suitable activity which is registered to this broadcast receives that update.
Using the push approach, it would mean that even if the app is killed, the push can still be triggered.
It seems a bit of a waste.
But considering that there might slow connection, connection drops, etc., the websocket is also quite "vulnerable".
Also, what if 24 hours of a story expire, how do I make sure I always remove that story from the Stories list view and not keep displaying it ?
With a socket, we may lose connection. if we reconnect after we were to supposed to be notified that the Story time expired, we would still display that Story.
Just store the stories in some sort of database, together with their creation dates. A client can then periodically fetch for new stories. Based on the creation date, the client can calculate the age of the story and decide to show it or not.
If a story has been seen by the client, he can store that information in local storage so watched stories can be displayed as old stories.
Now for the cleanup part, there are two options. Either you write a program that runs on a server 24/7, that like a client, periodically fetches the stories from the database, checks their age and deletes those which are too old. The other approach would use the same concept, but all normal clients delete old stories from the database.
The second option would be very insecure though, because the age calculation could be manipulated on the client side, either by mistake or by an attacker.
Related
I am new to mobile development and I undertook some freelance work. Now I am required to display a page with top N nearest places (businesses registered with us).
I am wondering what the best way of going about this is, from my experience it would seem that I would want to do this calculation in my back-end server.
I have a NodeJS server, however, it seems the server will be concurrently doing a lot of other work just verifying JWT tokens and what not, I have seen that android provides a method to easily do these calculations (Location). I have also read that there are some google API's one could use.
The idea I have is that I can pull my places with their lat, long from my database then the user sends lat,long and my server calculates top N results and sends to user.
What would you recommend and why?
Thanks!
The short answer is: Server.
But why?
The job of the server to make it so no one can use the app who is not authorized to do so. Thats one of many jobs anyway. However, you should send your location to the server, the server makes the API call, and returns the N closest locations.
You take that response, parse it, and fill in a view as you would like. With the server doing the heavy lifting, you can then also keeps track of something better, like how many calls, which calls, location of calls, etc etc.
I am developing a custom chat application.
Sent messages are stored on my server using a simple POST.
A service that runs in background is responsible to check using another POST every 5 seconds if there are new messages for the current user and send a notification to the him/her.
I am a bit worried about network and CPU usages because i am aware that internet connection is used all the day.
I used that approach because i need to manipulate some data on my server code before delivery a notification to receiver.
An example is that i have some users assigned as admin. The users username is their email. When a message is sent from one of the admins i overwrite their username (so their email) with the application name before delivery the notification to the receiver.
Is this the best approach to create a custom chat application?
Dont know exactly why you used this use case, but really simple solution provides Firebase.
You can read something about that here. Basically you have to:
Create some layouts
Add listener on "send message" click, that will add message to firebase realtime database
Create FirebaseListAdapter, who will display messages to user
From database docs
all of your clients share one Realtime Database instance and
automatically receive updates with the newest data.
From my understading you dont have to check every 5 seconds if there is some message waiting. Firebase handles it for you.
I am working on an Android APP that will handle multiple email accounts, POP3 and IMAP. The current APP, to check for email, you open the APP and click on an update feature. That logs into each email account, and checks for any new mail. Although this works, we need a more efficient, and timely check for new messages.
At first I thought PUSH was the way to go but from what I have read on here, that may not be ideal... especially with multiple IMAP accounts. If I understand right, it keeps each IMAP connection open long-term which could cause some other issues and not sure I could even use it with POP3 accounts.
I looked into POLL as an alternative but that too has potential issues. That involves how frequent it fires up and does the account "checks", the amount of time that takes, and how that all effects battery life. In addition, there appears to be several different "ways" to schedule the POLL option (AlarmManager, JobScheduler, and SyncAdapter)... and I assume one might be better than the other... especially with new version of Android that let you "quiet" your device at certain times of the day.
So the question is, based on what we are looking to do, which is the better way to handle this capability? I am leaning towards PUSH (which format...still unsure), but don't want to waste hours of time to find out that I went down the wrong path.
Thanks ahead for any advice.
IMAP supports IDLE Command in which client acts as passive entity depending/relying upon the server to notify arrival of new mails in account. But as you correctly figured out that it will keep connection open and other issues of rebuilding the connection if it gets disturbed due to any possible reason.
POP do not provide such passive capability to clients. Clients have to pull the mails from server.
From my experience, total synchronization of account on client side is not a problem but it requires too many things to be taken care of like parsing the mail content, maintaining the active list of mails in an account's folder, etc.
[1] Very simple synchronization mechanism would be to simply show the MailList and whenever a message is clicked upon then dynamically load the content of mail.
MailList = Sorted order of Mail Entries in an account's folder.
Single Mail Entry = Sender + Mail Subject + Mail Date & Time + Mail Size
This method of showing MailList initially and fetching the mail content on demand can be achieved via both:
IMAP (FETCH Subject, INTERNALDATE, Sender, Size) and
POP (TOP n m {TOP is a POP command which can fetch m number of lines of nth mail's body along with complete MailHeaderSection of nth mail}). Afterwards MailHeaderSection can be parsed and MailList can be constructed.
[2] Simply fetch the complete mail and store accordingly. Now, Parse it on demand and show it to user. This is also readily supported in both:
IMAP (FETCH BODY.PEEK[])
POP (RETR n)
I like this method since its simple.
Both the above methods are working as if client is active entity (pulling mails on demand).
I'm developing an Android application , i have to develop an activity that contains a simple communications board .
The information that i want show are contained in a database , When the user started the activity the informations are downloaded , parsed and Showed to users.
Now i would implement a function That notify to the user When a new information is inserted in the db.
Now what that i don't understand is:
I need to implement a service that runs in background and polls the database and notify to the user when a information is inserted or can i use only the GMC (now FCM) ?
If i don't want use GCM there are another solution to do this ?
I have read some thread in stack overflow and there are different solution but i don't understand what is the best solution for my problem.
Speaking at a high-level, here's what I would do.
If I didn't want to use GCM then I would write my own service to make an http request to my backend web server to check if any new information is available. If any new info is received save it to my local Android database and refresh the app. I'd use Android's AlarmManager to start this service every so often (hourly, daily, whatever frequency I want).
The situation: I have many real life locations with specific information associated with them, and updated frequently. I am unsure of how to store this information for use in an android application.
My original thought was storing the data on some server/cloud source/database, reading from the server from each Activity in the app to make sure the info is up to date, and update the server with any changes that may or may not have been made.
For example: there are 200 people inside the library, one person leaves.
So we would read the number of people from the server, display this on the app, person leaves, subtract one, send the new number back to the server.
Would this be an incorrect approach? I'm fairly new to Android in general, and I really have no experience on how to approach this type of situation, what services to use, etc.
I would look into using Parse, its a pretty sweet way to power the backend, and their website is very detailed in explaining how to use it.