How to display multiple notifications on different days?

I need help and I am a novice regarding notifications and alarms managers on different days and different notifications for example:

I have 2 calendars already defined

calendar.set (2020,5,15,0,0,0); notificationManager.notify(0, NotifyBuilder.build()); This is showing the notification "We remind you that you can request a process now"

calendar.set (2020,5,20,0,0,0); notificationManager.notify(1, NotifyBuilder1.build()); This is showing the notification "We remind you that you can request a process that ends in 10 days"

This code is of MainActivity()

Calendar c1 = Calendar.getInstance();
c1.set(2020,5,15,0,0,0);

Calendar c2 = Calendar.getInstance();
c2.set(2020,5,20,0,0,0);

Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pendingIntent);
        
Intent intent2 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 1,intent2, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am1 = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
am1.set(AlarmManager.RTC_WAKEUP, c2.getTimeInMillis(), pendingIntent1);

This code is a class AlarmReceiver()

public class AlarmReceiver extends BroadcastReceiver {

    int id = 0;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.notification)
                .setContentTitle("Notification")
                .setContentText("We remind you that you can request a process now").setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})

        notificationManager.notify(id, mNotifyBuilder.build());
        Log.i("TEST", "Count: "+id);

        long when2 = System.currentTimeMillis();
        NotificationManager notificationManager2 = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent2 = new Intent(context, MainActivity.class);
        notificationIntent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1,
                notificationIntent2, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri alarmSound2 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mNotifyBuilder2 = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.notification)
                .setContentTitle("Notification")
                .setContentText("We remind you that you can request a process that ends in 10 days").setSound(alarmSound2)
                .setAutoCancel(true).setWhen(when2)
                .setContentIntent(pendingIntent2)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        notificationManager2.notify(++id, mNotifyBuilder2.build());
        Log.i("TEST", "Count: "+id);
        }
    }

BUT IN THE NOTIFICATION OF THE DATE 06/15/2020 I GET THE TWO NOTIFICATIONS AT THE SAME TIME AND IN THE SAME WAY OF THE OTHER DATE 06/20/2020

The "id" is correctly of for .notify() but i dont now problem

2 answers

  • answered 2020-06-27 07:15 arttuOll

    At the moment you are creating two notifications in your onReceive()-method regardless of which intent triggered the broadcast.

    You need to check which intent triggered the broadcast (this is the Intent parameter in onReceive()) and then send an approppriate notification.

    You could do this, for example, by initialising your intent and intent2 in MainActivity with unique actions (for example, Intent intent = "new Intent("com.myapp.ALARM1")) and then checking in onReceive() for the actions and acting based on them. Note, that this would require you to set up intent-filters for your actions in your manifest file.

    So in your onReceive() you might have something like this:

    @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("com.myapp.MY_ALARM1")) {
                // notification here
            }
            if (intent.getAction().equals("com.myapp.MY_ALARM2")) {
                // other notification here
            }
    

  • answered 2020-06-27 09:15 JEMV

    Thanks to @Mike M's comment the solution is:

    Calendar c1 = Calendar.getInstance();
    c1.set(2020,5,15,0,0,0);
    Calendar c2 = Calendar.getInstance();
    c2.set(2020,5,20,0,0,0);
    
     
    
    Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
    intent.putExtra(Intent.EXTRA_TEXT, "We remind you that you can request a process now");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    
        am.set(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(), pendingIntent);
    
    
    Intent intent2 = new Intent(MainActivity.this, AlarmReceiver.class);
    intent.putExtra(Intent.EXTRA_TEXT, "We remind you that you can request a process thats end in 10 days");
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 1,intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am1 = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    
        am1.set(AlarmManager.RTC_WAKEUP, c2.getTimeInMillis(), pendingIntent1);
    

    Class ALarmReceiver

    public class AlarmReceiver extends BroadcastReceiver {
    
    int id = 0;
    String text = intent.getStringExtra(Intent.EXTRA_TEXT);
    @Override
    
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
    
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    
    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.notification)
            .setContentTitle("Notification")
            .setContentText(text)
    .setSound(alarmSound)
            .setAutoCancel(true).setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
    
    notificationManager.notify(id, mNotifyBuilder.build());
    
    }