android - Using Ottos bus for 'notification polling' -


on views have icon have red dot if user has notifications, created class (every few minutes) poll , see if have notifications. except @subscrtibe never getting triggered. 1 thing thought maybe cause start post before homeactivity registered timer didn't think problem?

public class notificationutils {      private static timer timer;      private static final int minutes = 1000 * 60;     private static context applicationcontext;      public static void starttask(context context){         checknotifications();         applicationcontext = context;         timer = new timer();         timer.schedule(task, 0l, minutes);     }      public static void killtask(){         timer.cancel();         timer.purge();     }      static timertask task = new timertask() {         @override         public void run() {             checknotifications();         }     };      private static void checknotifications(){         restinterface service = restservice.getrequestinstance(applicationcontext);         call<boolean> call = service.hasnotificatrions(sessionuser.getinstance().getuser().getid());         call.enqueue(new callback<boolean>() {             @override             public void onresponse(call<boolean> call, response<boolean> response) {                 sendnotificationimage(response.body());             }              @override             public void onfailure(call<boolean> call, throwable t) {                 log.w("timertask", "failed");             }         });     }      private static void sendnotificationimage(boolean hasunreadnotifications){         if(hasunreadnotifications) {             busprovider.getinstance().post(r.drawable.notification_alert_icon);          } else {              busprovider.getinstance().post(r.drawable.notification_icon);         }     } } 

when user logs in, start polling...

public class loginactivity extends appcompatactivity {     ....     successfullogin(){         ....         notificationutils.starttask(getapplicationcontext());     } } 

then homeactivity register class , subscribe..

public class homeactivity extends appcompatactivity {     ....     @override     protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_home);          busprovider.getinstance().register(this);     }     ....     @subscribe     public void setnotification(int imageresource){          notificationbutton.setimageresource(imageresource);           log.w("homeactivity", "setting resource");      } } 

edit.. adding busprovider class

public final class busprovider {     private static final bus bus = new bus();      public static bus getinstance(){         return bus;     }      private busprovider(){} } 

you should not use java primitive types event classes. can use:

public class displaynotification {     private int resourceid      public displaynotification(int resourceid){         this.resourceid = resourceid;     }      public int getresourceid(){         return this.resourceid;     } } 

change function signature be

@subscribe public void setnotification(displaynotification event){...} 

and post this:

busprovider.getinstance().post(new displaynotification (r.drawable.notification_icon)); 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -