0

I have a button, when I press it some data is fetched from FirebaseDatabase and then a notification is fired.

Here's how:

aReference.child(rID).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        if (dataSnapshot.getValue() != null) {
            Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
            uAU = newRequest.get("pName");  

            final int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

            Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
            notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationAU());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);
        }
    }
});

Here's NotificationARBroadcastReceiver.class:

public class NotificationARBroadcastReceiver extends BroadcastReceiver {

    public static String NOTIFICATION = "notification";
    public static NotificationManager mNotifyMgr;
    public static int m;

    @Override
    public void onReceive(Context context, Intent intent) {

        m = (new Random()).nextInt(10000);
        Log.d("mMain", String.valueOf(m));


        mNotifyMgr =
                (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotifyMgr.cancel(m);
        mNotifyMgr.notify(m, notification);

    }
}

Here's getNotificationAU():

private Notification getNotificationAU() {
    mBuilder = new NotificationCompat.Builder(getBaseContext())
                        .setSmallIcon(R.mipmap.app_icon_1)
                        .setContentTitle("title")
                        .setContentText(***);

    Intent resultIntent = new Intent(getBaseContext(), MainActivity.class);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(
                        getBaseContext(),
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

    mBuilder.setAutoCancel(true);
    mBuilder.setContentIntent(resultPendingIntent);
    return mBuilder.build();
}

The problem is that on pressing the button even once, notifications are fired multiple times and many same notifications are shown in the status bar.

Why is this happening and how to fire only one notification?

Please let me know.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • looks like you have multiple child under `aReference.child(rID)`, new notification is shown every `onChildAdded` is called – Wilik Dec 28 '16 at 08:29
  • @Wilik yes there are multiple child. So, how can I show the notification just once even under this circumstance? – Hammad Nasir Dec 28 '16 at 08:34
  • it depends on your notification purpose, you can use ValueEventListener instead, set the same id for every notification, or check if a notification is shown before firing new one. – Wilik Dec 28 '16 at 08:43
  • you should check whether notification is shown before firing up, actually it depends on your logic. – Hasif Seyd Dec 28 '16 at 08:44
  • how to check whether notification is shown before? – Hammad Nasir Dec 28 '16 at 08:45
  • you have multiple child? and for every child you are trying to show different notification or the notification is same for all child ? – Hasif Seyd Dec 28 '16 at 08:46
  • @HasifSeyd notification is same for all child – Hammad Nasir Dec 28 '16 at 08:46
  • then instead of using random id each time you show notification (new Random()).nextInt(10000), make it same id ,use some constant value – Hasif Seyd Dec 28 '16 at 08:47
  • [How to check which notifications are active in status bar](http://stackoverflow.com/questions/3630733/how-to-check-which-notifications-are-active-in-status-bar-in-android-dev) – Wilik Dec 28 '16 at 08:48
  • to check if notification is present,from API 23 you can use this method NotificationManager.getActiveNotifications(). This returns a list of active notifications launched by your app in a form of an array. – Hasif Seyd Dec 28 '16 at 08:53

1 Answers1

0

Adding mNotifyMgr.cancelAll(); (where mNotifyMgr is the NotificationManager) before mNotifyMgr.notify(m, notification); did the job for me!

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133