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;
        }
    }

I want to create a service that shows a notification when a new child is added to the "notify" tag in firebase. The issue is when the service gets started for the first time all the child under the "notify" tag gets notified through notification builder. Also after a particular set of period again this process is repeating even though no new child has been created under notify tag. I want to show notification only for the latest child added to the "notify" tag.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Abhinav Nigam
  • 21
  • 2
  • 7

1 Answers1

0

When you add a ChildEventListener to a Firebase reference, onChildAdded is called once to each child at the given resource. Otherwise, how would you populate a ListView or RecyclerView? ;-)

I suggest you add a createdAt and updatedAt field (using ISO 8601 format) to your JSON structure. Then you could query elements created after you add the event listener.

However it is a bad idea keep a listener running on a background service, because you will keep a socket opened for a long time, draining the device battery.

Community
  • 1
  • 1
JP Ventura
  • 5,564
  • 6
  • 52
  • 69