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"タグの下のすべての子が初めて通知ビルダーを介して通知を受けると、サービスが初めて開始されます。 また、特定の期間の後に、新しいタグが通知タグの下に作成されていなくても、このプロセスが繰り返されます。 「通知」タグに追加された最新の子の通知のみを表示します。AndroidベースのサービスでFirebaseを使用して通知する
しかし、なぜ通知が何度も繰り返しポップするのか教えていただけますか? –