10

The project I am working on has two different apps, 1 is server and other is client. Server app has 1 service class, which initiates server thread in onStartCommand() function. Class for server thread is defined in service class itself. Whenever server receives any data from client it stores in the database. So problem is, I want to notify user that new data has been arrived through notification or toast message whichever possible. Also tell me where should I write code for notification, whether I should write in service class or thread class or firstActivity class from which i am starting server service. Thank you.

Olli
  • 748
  • 6
  • 20
sagar
  • 1,738
  • 5
  • 27
  • 42
  • 1
    possible duplicate of [Sending a notification from a service in Android](http://stackoverflow.com/questions/1207269/sending-a-notification-from-a-service-in-android) – Reno Oct 09 '11 at 07:32
  • @Reno:I already referred this post, but i didn't understand where should i place that code, also that code is incomplete. I am beginner in Android. – sagar Oct 09 '11 at 07:47

2 Answers2

10

First you need to read this article on how to use Notifications.

Next use this to send a Notification, you can write this code in the service class at the point where you receive some data from the client.

NotificationManager notificationManager =
    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.notification_icon;
CharSequence notiText = "Your notification from the service";
long meow = System.currentTimeMillis();

Notification notification = new Notification(icon, notiText, meow);

Context context = getApplicationContext();
CharSequence contentTitle = "Your notification";
CharSequence contentText = "Some data has arrived!";
Intent notificationIntent = new Intent(this, YourActivityThatYouWantToLaunch.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

int SERVER_DATA_RECEIVED = 1;
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
Reno
  • 32,851
  • 11
  • 85
  • 99
3

Am I reading this incorrectly? Both your server app and your client app are both on the same Android device?

In any case, take a look at the ApiDemos for code for notifications through a service.

And since you're probably going to be dealing with a remote server (and not a local one) and all the latency issues associated with the network, I think you better wrap your service in an AsyncTask.

Also learn about Broadcast Receivers, registered Broadcast Receivers can be great way to trigger Services/Activities without the need to have your own Service polling in a loop indefinitely.

Stephan Branczyk
  • 9,144
  • 1
  • 29
  • 49
  • Thanks! my server and client app r running on different emulators, there is no problem in data transfer and storing it in database. I will look at Broadcast Receivers. Is there any way to detect there is change in database table? So that if changes found i can notify user from activity class. – sagar Oct 09 '11 at 07:35
  • As u said , i have used broadcast reciever, and it is working, but i want to send 1 string with broadcast and display that string in toast msg. I have used intent.putextra(). but at the receiver end i am recieving null value, i think problem is in my get extra code, can u plz tell me how to receive data send by broadcast intent. – sagar Oct 09 '11 at 13:35