Accumulate notifications in the status bar [duplicated]

0

I have a problem, which my app sends for example

  

2 notifications at the same time

of two classes that extend a service, but only keeps the last notification, does not accumulate to see both, I want to accumulate all notifications in the bar and that the user must remove.

The first class that extends a service

 @Override
    public void onDestroy() {
        Log.e(TAG2, "Timer cancelado");
        super.onDestroy();


        int icono = R.mipmap.ic_launcher;
        NotificationCompat.Builder mBuilder;
        NotificationManager mNotifyMgr =(NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        Intent i=new Intent(ServicioTimer2.this, tiempo_carro2.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(ServicioTimer2.this, 0, i, 0);

        mBuilder =new NotificationCompat.Builder(getApplicationContext())
                .setContentIntent(pendingIntent)
                .setSmallIcon(icono)
                .setContentTitle("Carro 2")
                .setContentText("Tiempo de carro 2 terminado!")
                .setVibrate(new long[] {100, 250, 100, 500})
                .setAutoCancel(false);

        mNotifyMgr.notify(1, mBuilder.build());

    }

The second class is the same previous code, they only change the message.

    
asked by Ashley G. 08.09.2017 в 00:38
source

1 answer

0

The NotificationManager#notify() method receives a int that is the id of the notification. You must specify a different id for each notification that you will show in the method mNotifyMgr.notify(1, mBuilder.build()); and the system will create them independently.

For example:

@Override
    public void onDestroy() {
        Log.e(TAG2, "Timer cancelado");
        super.onDestroy();


        int icono = R.mipmap.ic_launcher;
        NotificationCompat.Builder mBuilder;
        NotificationManager mNotifyMgr =(NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        Intent i=new Intent(ServicioTimer2.this, tiempo_carro2.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(ServicioTimer2.this, 0, i, 0);

        mBuilder =new NotificationCompat.Builder(getApplicationContext())
                .setContentIntent(pendingIntent)
                .setSmallIcon(icono)
                .setContentTitle("Carro 2")
                .setContentText("Tiempo de carro 2 terminado!")
                .setVibrate(new long[] {100, 250, 100, 500})
                .setAutoCancel(false);

        Random r = new Random(); // id random para notificacion
       int randomNo = r.nextInt(1000+1);
      mNotifyMgr.notify(randomNo, mBuilder.build());

    }
    
answered by 08.09.2017 / 00:46
source