0

I implemented a notification builder inside a childEventListener in order for my users to receive notifications when new posts are available... the notification builder method is:

public void showNotification() {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.app_logo)
                        .setContentTitle("new post")
                        .setContentText("Check it out guys");
        Intent resultIntent = new Intent(this, MainActivity.class);
        android.support.v4.app.TaskStackBuilder stackBuilder = android.support.v4.app.TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int mId;
        mId = 1;
        mNotificationManager.notify(mId, mBuilder.build());

    }

and inside the onCreate() method I have

mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts"); 
mDatabasePosts.addChildEventListener(new ChildEventListener() {


            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                showNotification();
            }

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

            }
        });

my users are notified when a child is added to the database but they are also receiving that notification when they load the app... I tried implementing this on the onPause() but then the notification appear everytime they press the home button. Please advise where do I go wrong.

Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jagmaster
  • 107
  • 4
  • 12
  • are you getting old post notification in this call? – Jd Prajapati Mar 27 '17 at 14:00
  • hi @JdPrajapati hmm actually i don't know... but the notification is called when the app is created... as well as when a new child is added on the database... but NOT onResume(); – Jagmaster Mar 27 '17 at 14:07

2 Answers2

0

By default, despite the name of the method, when you get a DatabaseReference, firebase gets all data of the desired child and returns. What you need to do is:

  1. Set a haveNewItems = true; in your class
  2. Change the onChildAdded method to this:

    @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { if(!haveNewItems) showNotification(); }

  3. Add a singleValueListenner and on completion, set the haveNewItems to false.

The approach in js can be read here

Community
  • 1
  • 1
0

I've managed to figure it out. In case some of you are stuck as well this is a good way to do it...

so in your activity, create a class

public static boolean isBackgroundRunning(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        //If your app is the process in foreground, then it's not in running in background
                        return false;
                    }
                }
            }
        }


        return true;
    }

Credit Mihir Patel for this beautiful piece of work!

The code above will check if app is running in background or foreground

Then simply add that logic inside the onChildAdded method such as:

mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts"); 
mDatabasePosts.addChildEventListener(new ChildEventListener() {


            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if(isBackgroundRunning(MainActivity.this)){

                showNotification();
                }
            }

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

            }
        });

and voila, you will only get notified when your app is in the background.

Jagmaster
  • 107
  • 4
  • 12