【问题标题】:Attaching listener to FirebaseDb in FirebaseJobDispatcher在 FirebaseJobDispatcher 中将侦听器附加到 FirebaseDb
【发布时间】:2017-01-03 14:33:44
【问题描述】:

我正在使用 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;
}

}

谢谢

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-job-dispatcher


    【解决方案1】:

    显然,为什么从未调用过侦听器的原因是我的代码在显示通知的地方不断抛出空指针异常,因此它从未达到我附加侦听器并侦听数据库中的更改的地步. 移动代码以在onChildAdded 中显示通知,为我修复了它。

    【讨论】:

      猜你喜欢
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2014-12-01
      相关资源
      最近更新 更多