i am developing chat application. I want to automatically refresh the chat on receiving notification. I have tried using timer but its not that good as it refreshes after certain time period.
Asked
Active
Viewed 844 times
-3
-
Did you try anything ? – Rakshit Nawani Mar 28 '16 at 11:03
2 Answers
1
You can use Service and Broadcast Receiver in your notification receiver class. When you received the notification, send the broadcast. Add the data into your list and set it to the adapter.

Muhammad Umair
- 583
- 11
- 32
-
-
@PrachiKarapurkar [How to pass data using broadcast](http://stackoverflow.com/questions/10032480/how-to-pass-data-to-broadcastreceiver) – Aks4125 Mar 28 '16 at 11:47
-
1
-
@MuhammadUmair you should not do any heavy task in broadcast receiver... better way is to start a service and do your work. – SRB Bans Mar 28 '16 at 13:28
-
@sourabhbans you are right but according to her requirement i think broadcast receiver will be the best option. she only can add data to list and call adapter.notifyDataSetChanged. – Muhammad Umair Mar 29 '16 at 04:27
-
@MuhammadUmair agree with broadcast receiver.. but she need to store data to database also.. In that case she need to start service >> put the data in database(can be performed extra work there like network operations to download the data) >> use the `LocalBroadCastReceiver` to update activity/list... – SRB Bans Mar 29 '16 at 12:12
-
@sourabhbans I agree with you on this point, and I think async task would be a good option too. – Muhammad Umair Mar 29 '16 at 12:14
-
@MuhammadUmair Nope... You shouldn't use background tasks with broadcast receivers. Broadcast receiver component is considered destroyed as soon as it returns from its onReceive() function. And if this was the only component in process, the process can get killed at any time. – SRB Bans Mar 29 '16 at 12:23
-
Your process will be killed after onReceive method returns only if there is no other activity or service in your app is running. If your broadcast receiver is an instance of an inner class and only receive when your activity is active, then your process will not be killed after onReceive method returns. – SRB Bans Mar 29 '16 at 12:24
-
Oh yes, i got your point. She should use Service and broadcast receiver. – Muhammad Umair Mar 29 '16 at 12:25
-
-
0
You can do something like this...
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String code = data.getString("code");
Log.e(TAG, "From: " + from);
Log.e(TAG, "Message: " + message);
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
Noty_bean noty = new Noty_bean();
noty.setTitle("Title");
noty.setMessage(message);
noty.setTimestamp(System.currentTimeMillis());
noty.setState(0);
noty.setNoty_id("default id");
Intent intent = new Intent(this,Your_refresh_service.class);
Bundle bundle = new Bundle();
bundle.putSerializable("noty",noty);
intent.putExtra("bundle",bundle);
startService(intent);
// sendNotification(message);
// [END_EXCLUDE]
}

SRB Bans
- 3,096
- 1
- 10
- 21
-
-
notification data m saving in database n from local db m retrieving and setting to list how can i update the recycle view with new data – Prachi Karapurkar Mar 29 '16 at 09:36
-
@PrachiKarapurkar You need to update the activity from your service.... check it.. http://stackoverflow.com/a/4739898/3790052 – SRB Bans Mar 29 '16 at 12:06
-