0
public class MyService extends Service {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference().child("notify");
// NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public void onCreate() {
super.onCreate();
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue().toString();
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Society")
.setContentInfo(value)
.setVibrate(new long[]{500, 500}).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(value);
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, mBuilder.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) {
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
}
我想要創建一個服務,以便在將新子項添加到firebase中的「notify」標記時顯示通知。 問題是服務第一次開始時,通過通知構建器通知「通知」標記下的所有孩子。 同樣在一組特定的句點之後,即使在通知標籤下沒有創建新的孩子,該過程也會重複。 我想只顯示添加到「notify」標籤的最新孩子的通知。使用Firebase的服務的Android通知
但是,你能告訴我爲什麼通知一次又一次地間隔彈出? –