Send data from Server to Android Database - android

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

Related

Receive notifications on android upon database change

I have an android app which is capable to interact with a (for now local, but in future online) RESTful database (using PHP and Slim framework).
A registered user can create/upload new text, view all its texts, delete and modify them.
The user can log through different devices (Android smartphones for now, but in future with Desktop computers as well) and perform said operations.
I need that EVERY device (on which the user is logged in) is notified upon each change in the database.
What is the best approach to implement such notification capability either on the PHP/MySQL server and in my android app?
I have heard about Google's Firebase Cloud Messaging but i'm not sure neither if it is the easiest and fastest way to do it, nor if it is compatible with my already working environment.
Please guide me in the right direction as i don't have a clue on what is best for my case.
You have to registered mobile device or desktop to fcm/gcm server or apns in case of iOS . When database is updated on server then it will get a trigger so on after save will send notification push with your payload.

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).

android - when to do GCM registration

I have a android client app, a server side in django and now I am adding push notifications with GCM.
In my app I have users that login/logout, so I will have a table in my database with devices ids coupled with users, so that every time I want to notification a user, I will lookup its device id and send the notification.
my question is:
When is best to register the Device to the GCM?
When to add the Device to my Server side database?
In the Google Docs it says you should do it once, when the app is installed.
Because I have users in my app, and I want to couple a user with a device in the database, when the app is installed there still isn't a user to associate the Device with.
After the user installs the app, he can register, login and so on.
So when you think is best to register the Device to the GCM and to my server side?
Should it be with a user or only associate it later?
Thanks a lot!
My suggestion would be:
Register the device immediately after the first execution
Save device id somewhere accessible anytime by the app
Couple the id with user details after registration
With this approach you will be able to handle issues if something goes wrong with the registration process and send a notification to the device even if the user is not registered.
EDIT:
You should also implement a strategy to check on a regular basis if the association between the user and the device is still valid or needs to be updated

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)

seen feature in a android chat application

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.

Categories

Resources