seen feature in a android chat application - android

I am developing a chat application in android in which 2 users chat with each other, it is based on sockets.I want to implement the seen feature just like facebook or whatsapp.
I think may be there is an onfocus method associated with an activity so that whenever user opens the chat activity I can set the latest messages as SEEN in the DB, is there any focus method associated with an activity?
2-Do I have to store messages on local sqlite or on mysql remote database? If I will store on local server, how quickly I have to replicate/update remote DB?
Thanks

In Android the onFocus method you talk about would be onResume(), which is called whenever your Activity goes to the foreground.
Ideally the messages are stored on local database only, there's no point in replicating the messages on a remote server. However this depends on how you want to manage your chat.
Facebook, for example, is obviously server based, meaning that you can see your Facebook messages on any device just by logging in. WhatsApp, on the other hand is client based, and if you buy a new phone and install WhatsApp you don't see previous conversations' messages.
Server based messaging is more complicated because you need to replicate messages, but how often is the wrong question, because it's not time based. As soon as the user connects, you replicate, and store the last n messages locally.
Client based messaging doesn't need replicating, just deliver the messages and you're done. Unless you want a user to be able to send messages when his peer is offline. Then you store the messages in the server, and once the recipient connects, you forward the saved messages and delete them from the server.

Related

Let multiple users sign in at the same time on 1 account

I am developing an android app where a user can register and login. Is there any way that multiple device can login to that account simultaneously and syncing with each other?
It's similar like Facebook where you can have multiple devices logged on 1 account at the same time. If i for example change my profile name on facebook from 1 device, then all other device will show that my profile name is changed.
Yes it is possible, in order for both users to be able to sync however, your best bet is a server that holds active connections to both devices or communicates with them in some other way.
Your devices could have an active connection to the server via sockets and every update sent by one client makes the server inform all other connected clients about having to update their state.
Alternatively you could also use push notifications to update your clients about updates that have happened on a different client.
Consider a chat, where people A,B,C are in the same group. When A sends a chat message, it is sent to the server and people B and C are then informed by the server about the new chat message arriving, ideally over an active connection (socket). Also they have to download all updates once they freshly connect to the server (the ones that happened while they weren't connected), in case they were offline at the time the message of A was received by the server.

Firebase push notification vs childAdd listener

I'm working in a friend request module for mobile app base on Firebase so I'm considering to choose the way that notification is pushed.
Assume that userA request to be friend with userB. There're 2 ideas now:
- userA send request to a simple server then it will call FCM to send notification to userB.
- Make a service that listen to data changed in Firebase realtime database then userA will make change on that db and notification will be shown on userB device.
I think both are possible to implement but what is better, and why?
Please give me some advice about this..
Thanks in advance.
Using either one should be fine.
However, a point to consider here is when keeping a listener active for the Real-time database, it also keeps an open socket on the user's device which adds to battery consumption.
While for FCM, it will only trigger once there is a notification is needed to be sent. If a friend request isn't really that app critical, I think using FCM is a way to go.
Have you also considered using both? If the user is currently online, it would be good to use the Real-time DB, but the childAdded won't be triggered if the user is offline (not using the app for instance). In that case, you can set it so that a notification will be sent to the user.
The important thing in your scenario is that the friend request should be saved first in your database or app server, so that it will trigger the corresponding action (FCM notification or Real-time DB update).

How does GCM work ? (google cloud messaging for Android)

I have an android app which i connect to my server using REST API (django rest framework)
here is a scenario(and maybe my plan):
data is sent back and forth as json
I have a user model and a task model where users are owners of some task.
Users typicaly sends a task to another user (with json similar to this: {"owner": "exampleuser", "from":"otheruser", "content":"example" ...} using POST method)
-The tasks has a boolean field "completed" and is deleted once the task is completed (using PUT or PATCH method: completed = true,)
once a new task gets created using POST method, the only way users can see any activities concerning their tasks is through an android activity that uses GET method to get a list of all the tasks owned by the user, by looking up all objects owned by the user
So my questions are:
Instead of having the user to check the app everytime. How can I use GCM to push notify the user?
How will it tell which user or device to send the push notification to?
How does GCM knows when there has been a change to tasks or when a user POST a task?
Android keeps one active connection to Google's servers, but it doesn't use much power or data, because no traffic is sent along it until something sends a GCM message to an app on your phone. There's only one connection on the phone, used by all apps: installing a new app that uses GCM doesn't add any extra load.
The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app. The app must have registered with Android to use GCM, and it must have the relevant permission. When the app starts, it might create a notification straight away with the data from the message. GCM messages are very limited in size, so the app might instead open a normal connection to the third-party server to get more information (for example, downloading the headers of new emails).
The advantage of using push notifications is that apps don't have to run at regular intervals to check for new data, saving both power and data. The advantage of having a centralized mechanism like GCM is that the device only needs one open network connection and the Android GCM system is the only thing that needs to keep running, rather than each app having to stay running in the background to keep its own network connection to its own server.
As per the GCM implementation, it requires that you implement a remote server which will manage all the requests, both incoming and outgoing. You can do this simply with a web server acting as a webservice, which will get (for instance) the requests from the clients with a HTTP POST request, and process act consequently.
Instead of having the user to check the app everytime. How can I use GCM to push notify the user?
This will be managed by the server that I just described. It will know who is subscribed and who should receive the notifications.
How will it tell which user or device to send the push notification to?
Same goes here. The server, upon a subscription, should store the users in some kind of storage (a SQL database, for instance), and this way it will know who to send notifications. This implies you'll have to implement some timeout mechanism. In my case, I make clients send a dummy HTTP POST every 30 seconds. If I don't get this request from a reasonable amount of time, I consider the client timed-out and therefore I remove them from the database.
How does GCM knows when there has been a change to tasks or when a user POST a task?
Same story, it's all handled by the server. You implement the logic of what should happen upon any request.
You might want to read this: How to send location of the device on server when needed
And also this references:
Reference on Google Cloud Messaging
Android Push Notifications using Google Cloud Messaging GCM - Android Example
Google Cloud Messaging using PHP
Connection between PHP (server) and Android (client) Using HTTP and JSON
Notificaciones Push Android: Google Cloud Messaging (GCM). ImplementaciĆ³n Cliente (Nueva VersiĆ³n) (spanish)

Using GCM for device to device app data sync

I am planning to build a shopping list application, wherein grocery items from one user (tied to one google account on the smartphone) can be sent over to another app user (tied to another google account on his/her smartphone).
Seeing GCM, I am not sure, if we can do a device to device kind of data sync? Any ideas on how this sort of stuff is basically implemented in a quick and easy way would be highly appreciated.
you dont necessarily need to use GCM or any messaging service, all gcm is meant to do is notify a device that something has changed on your server and that you need to go get all the new stuff.
that being said all you need to do is just have a sync button that the user can manually sync the list from the server to the device and vice versa which would get rid of the need for GCM.
if you wanted automatic syncing then GCM or any other messaging service would be what you want
To elaborate on tyczj's answer, you will need a server anyway to store GCM registration ids from your users. So you can use the same server to host groceries items for them.
So the flow will be like this:
User A wish to send groceries list to User B, he uploads groceries list via your server's Web Service API, along with User B's google email address.
Your server has a list of GCM registration ids, which will be associated with their google email address. The server finds the GCM regid of user B, and push a notification with a URL to download the groceries list to him.
User B's phone receives GCM message, display a notification to let the user download the groceries list from the URL in the GCM message.
GCM is only needed if you want this 'real-time' way of syncing the list. You could also omit GCM, in which case, User B will only know User A has shared the list with him, when he runs the app (i.e. every time app is run, it will perform 'is there someone sharing a list with me' command).

Send data from Server to Android Database

I have an android app which receive data from web service and insert it to android's local database. This data flow is begun with a button in app's admin panel. I mean, an author login to admin panel in device and click the "Start Sync" button and device's database start to sync with SqlServer database.
I want to do is trigger this event from remote server. In other words, I want this process to begin as if someone clicks the "Start Sync" button, when I click the button in an web application. Let this web application is server and the devices are clients. I want this process to be triggered for all clients (broadcast).
How can I achieve this ? Thanks.
Have the clients (app) register to your server as GCM clients. The server can then send a request to all registered clients to update. In your app, you will receive an intent. Here's how to get started.
Just a hint.
I also once wanted to implement a similar strategy to force updates on clients.
In the end I switched to the app and user deciding when to download the data. (on startup and an alarmmanager which controlled a non persistent Service for daily updates).
A lot of developers forget the users, I've seen many applications with persistent services, and more undesirable behavior. Just because you can control users or program easy.
Otherwise Google Cloud Messaging is the way to go, as 323go mentioned

Categories

Resources