'notification polling' 使用 Ottos 总线
Using Ottos bus for 'notification polling'
在我所有的视图中,如果用户有通知,我有一个图标会有一个红点,所以我创建了一个 class 来不断(每隔几分钟)轮询并查看他们是否有通知。除了 @Subscrtibe
永远不会被触发。我认为可能是原因的一件事是我在注册 HomeActivity
之前启动了 post 但是使用计时器我认为这不会成为问题?
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);
}
}
}
当用户成功登录时,我开始轮询...
public class LoginActivity extends AppCompatActivity {
....
successfulLogin(){
....
NotificationUtils.startTask(getApplicationContext());
}
}
然后我的 HomeActivity 我注册 class 并订阅..
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");
}
}
编辑..添加 BusProvider class
public final class BusProvider {
private static final Bus bus = new Bus();
public static Bus getInstance(){
return bus;
}
private BusProvider(){}
}
您不应将 Java primitive types 用作事件 类。您可以使用:
public class DisplayNotification {
private int resourceId
public DisplayNotification(int resourceId){
this.resourceId = resourceId;
}
public int getResourceId(){
return this.resourceId;
}
}
将您的函数签名更改为
@Subscribe
public void setNotification(DisplayNotification event){...}
和post它是这样的:
BusProvider.getInstance().post(new DisplayNotification (R.drawable.notification_icon));
在我所有的视图中,如果用户有通知,我有一个图标会有一个红点,所以我创建了一个 class 来不断(每隔几分钟)轮询并查看他们是否有通知。除了 @Subscrtibe
永远不会被触发。我认为可能是原因的一件事是我在注册 HomeActivity
之前启动了 post 但是使用计时器我认为这不会成为问题?
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);
}
}
}
当用户成功登录时,我开始轮询...
public class LoginActivity extends AppCompatActivity {
....
successfulLogin(){
....
NotificationUtils.startTask(getApplicationContext());
}
}
然后我的 HomeActivity 我注册 class 并订阅..
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");
}
}
编辑..添加 BusProvider class
public final class BusProvider {
private static final Bus bus = new Bus();
public static Bus getInstance(){
return bus;
}
private BusProvider(){}
}
您不应将 Java primitive types 用作事件 类。您可以使用:
public class DisplayNotification {
private int resourceId
public DisplayNotification(int resourceId){
this.resourceId = resourceId;
}
public int getResourceId(){
return this.resourceId;
}
}
将您的函数签名更改为
@Subscribe
public void setNotification(DisplayNotification event){...}
和post它是这样的:
BusProvider.getInstance().post(new DisplayNotification (R.drawable.notification_icon));