我正在使用 Firebase Job Dispatcher 执行一个定期任务,该任务将每 15 分钟执行一次,并检查 firebase 数据库中的数据并显示有关最新数据的通知。
我面临的问题是,侦听器没有正确连接onStartJob
,即onDataAdded
永远不会调用内部的日志记录语句。
我在下面链接了我的代码,
public class FetchInfoService extends JobService {
InfoObject note;
NotificationCompat.Builder notificationBuilder;
private static final String TAG = "SyncService";
public FetchInfoService() {
}
@Override
public boolean onStartJob(com.firebase.jobdispatcher.JobParameters job) {
Log.e(TAG, "Executing job id: " + job.getTag());
DatabaseReference dbReference = FirebaseDatabase.getInstance().getReference().child(CONTENT);
Log.e(TAG,dbReference.getKey());
dbReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG,"OnDataChanged Called"); //----Never Called
for (DataSnapshot infoDataSnapshot : dataSnapshot.getChildren()) {
note = infoDataSnapshot.getValue(InfoObject.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG,"Listener Cancelled");
}
});
notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_delete_black_48dp)
.setContentTitle(note.getTitle())
.setContentText(note.getDescription());
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notificationBuilder.build());
return false;
}
@Override
public boolean onStopJob(com.firebase.jobdispatcher.JobParameters job) {
Log.e(TAG, "Finished job: " + job.getTag());
return false;
}
}
谢谢