How to send a notification in an android service? - android

In my android application, I have a server part containing a mysql database. I want to create a service whose role is to detect an update of a field. When the service detects a change in a table, it sends a notification to the user. I have not started writing the code but I have some questions to understand how the service works:
1) For access to the database I used AsyncTask, do I have to call this class in the onStartCommand method of the service ?
2) and for sending the notification, do I write its code in the onPostExecutemethod ?
3) Is the use of BroadCast mandatory ?
Thanks.

I would like to advise you to implement Firebase FCM notification to not consume the android resources for nothing.
good luck.

I found a solution by using BroadCast,Service and AlarmManager.

Related

Listview update automatically

I am new in Android Development. I am fetching my database from the Xampp server and display data on listview it's working fine but when I update my database I need to refresh my activity every time. I want that I should not have to refresh it, again and again, it should automatically update the data in listview as a kind of real-time something! How can I achieve this? Please, someone, help me out... Thanks in advance!
Use notifyDataSetChanged() with the adapter instance that you have created for the List view.
This will notify any change in the data model fetched from the database.
Create a Service to listen to the port continously as per your need whenever there is a change in the data, you can send a signal to the application to fetch data again and then notifyDataSetChanged() will update the changes.
work of your refresh functionlity will be done by Service(not Activity).
Use push Notification concept
Android Push Notifications using Firebase Cloud Messaging FCM & PHP
when the server update send push notification from server and receive application by using services .
Inside activity make method to refresh listview call this method in the service.
I think Web socket and push notification both are a Right way to solve your problem. But as per my opinion, you should go with push notification it would be good because Socket is more costly to do it for that you need to make server strong. When traffic increased in the server then the number of socket connection would be increased and you need to manage that much of socket connections. FCM/GCM push notification is easy for that. You just need to send silent push whenever update data regarding particular user only.
For this best suit Firebase database if it is suitable for you in your current state.

Perform code when the Android application is closed

I'm making an application that detects incoming text messages.
When a message is received, the application must perform a certain action, depending on the sender and the content.
The problem is that the application must work at all times.
Detecting messages works through a BroadcastReceiver class. Via a Toast message I can see that the application (open or closed) receives the message.
The problem then is that I must perform certain actions, which are stored in a local database (DB40). But I can't access the database when the application is closed.
So, how can I perform database access and run other code (like making the phone vibrate, or play a ringtone) when the application is closed?
Thanks in advance
You can access the DataBase... because you are getting the context from the receiver, from the context create the instance of the Database and go further according to your requirement..
Check this...
You need to run a service to perform all the task even app is closed.
Just go through with few sample and study:
http://developer.android.com/guide/components/services.html
Do you know about Services?
You can use service as back-end to perform action on incoming messages or can use database.
Database need only 'Context' object for read/write and service provide 'Context' object by 'getApplicationContext()' method.
If you have any ambiguity about answer, just give me your little code, i will give you back with solution.

how to write a tigger in sql server to send a push notification (GCM) to android phone?

i have applied the demo code of GCM for server side on appache server and for client side on android device and it is working great; but i need to activate this service in my application as follows:
i have a database on SqlServer and need to automatically send a push notification to android phone whenever some certain data gets modified, i think i should use an after update and after insert trigger to do this, but i don't know how to do it.
any help will be appreciated, thanks in advance.
You should not do it from a trigger. Adding the latency of a GCM push to each table update will quickly bring the performance to unbearable lows. You have to decouple the trigger from the GCM call, and the best way to achieve it is via a queue.
You can use a table as a queue and have an external process monitor the queue and handle the GCM call.
You can use Service Broker and deliver the GCM call from an internal activated procedure or from an external process that monitors the queue
You can use MSMQ and monitor an NT queue from an external process.
My recommendation would be to go with first option as is the simplest and has the inherent robustness of simplicity. Read the linked article to understand what is required to use tables as queues, and do not cut corners.
All options still rely on a trigger to enqueue the notification, but it will be a local enqueue, not a GCM call.
I'm sure someone will think of the naive solution of invoking GCM from the trigger itself using SQLCLR: don't do it.

How to create a service that check message has come from server in Android?

I want to create a background service which continuously check that is there any message that has come from server.
I am trying using AsyncTask but I am not getting how can I know when any message will come or has arrived.
Please Help.
I take it that you are polling at a set inerval to your web server to figure out the state. Simply read the response from your web server and take action.
Read this post
Example: Communication between Activity and Service using Messaging
Alternately try using the Cloud to device messaging.
If you want to go really large scale use XMPP.
I got the way from here.

What is the best way to implement a "stub" on Android?

I'm creating an Android application that will register an Observer and listen for events, I will probably use one of the suggestions by Mark in this previous question.
However, my question is, how can I create a "stub" on Android that I can use to fire events at my applications observer? For the purpose of this example, we'll assume that my app is listening for a wifi signal strength, I need to create something that will run on the emulator (or device) that will mock up data to "pretend" it has a strong/weak signal, and to relay that to my app.
Any suggestions?
I am no Android expert but here is my catch.
If you are implementing an observable, I believe you need to create a Service by inheriting from ServiceBase.
Also create a content provider and allow other applications to insert data. The whole notification is built into the framework so if you have a cursor, you will get notifications of change in the data.
So here are the steps:
You run the service and register for notifications
Application is getting an instance of your service and registers to get back a token
They use your content provider to insert event along with the token they got
They call the notification
You will be notified whenever anything changes.
I know that there are notification services built into the framework but I have never had a chance to look into it.

Categories

Resources