2

in my android application i am trying to implement childEventListener of firebase database even when the app is not active. means whenever the firebase db particular child has a new entry it should notify the user.. so till now i have done with this,

i have declared service in manifest file as

<service android:name=".ChildEventListener"/>

then my ChildEventListener class is as follows

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) {

        }
    });
}

}

but i tried, but no luck... even the new child is added, i am not getting any notification.... not even when the app is in foreground...

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
av development
  • 53
  • 1
  • 10
  • Are you sure that the service is started? Instead of notifications, maybe you can initially try with logs to see where the problem is: Is it at the service, Firebase listener or notification? – merterpam Oct 07 '17 at 09:29
  • ohh thank you much....my mistake...i forgot the start the service – av development Oct 07 '17 at 11:42
  • but now the problem is, i am getting notification only when app is active... service is not working in background...and also...the notification is also getting for only first entry... if i added one item..it gives notification. suppose then i cancel the notification by swiping it...and again added a item...its not giving new notification. why its happening? – av development Oct 07 '17 at 11:44
  • no no...i resolved my issue...it was again my mistake... but thanx once again..starting service was actually my issue... you saved my day – av development Oct 07 '17 at 12:50

2 Answers2

1

actually we can start a service in manifest fie. but in my case my mistake was i didnt added start service statement where i actually want to start it. so just by adding

startService(new Intent(this, ChildEventListener.class));

in a class where i want to start it. solved my problem.

-Thanx @ merterpam, for pointing out my mistake

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
av development
  • 53
  • 1
  • 10
0

There is another issue. You have not given the permission in the manifest. add this:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38