1
在我的android應用程序我試圖實現firebase數據庫的childEventListener,即使應用程序不活動。 意味着每當火力分貝特別的孩子有一個新的條目,應該通知用戶.. 所以到現在我有這個工作,android-firebase childEventListener在服務
我已經宣佈在manifest文件服務爲
<service android:name=".ChildEventListener"/>
然後我ChildEventListener類是如下
public class ChildEventListener extends Service {
FirebaseAuth auth;
NotificationCompat.Builder builder;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
retrivemsg();
return START_STICKY;
}
public void retrivemsg()
{
DatabaseReference mdb= FirebaseDatabase.getInstance().getReference("messages/"+auth.getCurrentUser().getUid());
builder=new NotificationCompat.Builder(this,"onchildadded");
mdb.addChildEventListener(new com.google.firebase.database.ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String msg=dataSnapshot.child("msg").getKey();
builder.setSmallIcon(R.drawable.send);
builder.setContentTitle("child added");
builder.setContentText(msg);
NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
,但我試過,但沒有運氣...甚至NE w孩子被添加,我沒有得到任何通知....即使當應用程序在前臺...
您確定該服務已啓動嗎?也許您最初可以嘗試使用日誌來查看問題的位置:它是否位於服務,Firebase偵聽器或通知? – merterpam
ohh非常感謝你....我的錯誤...我忘記了開始服務 –
,但現在的問題是,我只有當應用程序處於活動狀態時纔會收到通知...服務不在後臺工作......還...通知也只是第一次進入...如果我添加了一個項目..它給出通知。假設然後我通過刷卡來取消通知......並再次添加了一個項目......它沒有給出新的通知。爲什麼它發生? –