I have the following code for polling the unread notification count every X seconds from a server
I start the this process via ScheduledThreadPoolExecutor in the App.onCreate() and
Log.d("XXX", "Requesting Notification count from server ...");
is called once (i can see in Logcat), but neither of the two Retrofit call back functions getting called (and in fact no Retrofit debug logs). Morever, the "Requesting Notification count from server...." is never printed again (i.e. the periodic task is not running)
I am using Retrofit for other webservice calls as well (upon user input) and they are working fine (I can see the incoming and outgoing requests/responses in the logcat)
public class App extends Application {
private ScheduledExecutorService scheduleTaskExecutor;
...
@Override
public void onCreate() {
super.onCreate();
//region Set up the periodic notification count listener task
scheduleTaskExecutor= Executors.newScheduledThreadPool(2);
scheduleTaskExecutor.scheduleAtFixedRate(new PeriodicNotifCountFetchTask(), 0, 5, TimeUnit.SECONDS);
//endregion
}
class PeriodicNotifCountFetchTask implements Runnable {
@Override
public void run() {
Log.d("XXX", "Requesting Notification count from server ...");
EMRestClient.getmEMRestService().getNotificationCount(new Callback<NotificationCount>() {
@Override
public void success(NotificationCount response, Response unused) {
int unreadNotifCount = response.getCount();
Log.d("XXX", "Successfully fetched notification count, unread = " + response.getCount());
if (unreadNotifCount>0){
// call listener to repaint menu
for (NewNotificationListener x :notifListeners){
x.onNewNotificationReceived(response.getCount());
}
}
}
@Override
public void failure(RetrofitError error) {
Log.d("XXX", "Failed to fetch notification count from server");
}
});
}
}
}
@POST("/notification/notification_count/")
void getNotificationCount(Callback<NotificationCount> callback);
There may be some exception will be occuring because of which subsequent calls will be suppressed. ScheduledThreadPoolExecutor only "ticking" once
So try to put your code inside runnable of runOnUiThread like Scheduling recurring task in Android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With