1

I want to generate notifications for every 24 hours for which I've implemented the code, but on runtime, the notification is appearing multiple times on a single day. I.E. When the notification appeared in the panel and I swiped it off the panel. The same notification should appear on the next day and not multiple times on the same day.

Here's the code:

Calendar cal = Calendar.getInstance();
        Date d = cal.getTime();
        DateFormat df = new SimpleDateFormat("dd");
        String date_str = df.format(d.getTime());

        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute= cal.get(Calendar.MINUTE);

        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, 0);




        intent.putExtra(REQUEST_CODE_STRING, requestCode);
//      cancelTrigger(context,requestCode,intent);
//      cancelTriggerAlarm(context);


        PendingIntent sender = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(),
                1000 * 60 * 60 * 24, // 24 hrs in milliseconds
                sender);
phrogg
  • 888
  • 1
  • 13
  • 28
pavan teja
  • 13
  • 3
  • Please refer this https://developer.android.com/training/notify-user/group and this https://stackoverflow.com/questions/33040737/how-to-group-android-notifications-like-whatsapp – Rajesh Aug 29 '18 at 10:34
  • In your broadcast receiver mark the timestamp when you show the notification, and later check the current date with that timestamp if its the same day don't show it show it only when the day is next day – Cyph3rCod3r Aug 29 '18 at 10:35
  • Hi @Dr.aNdRO, Thank you for your response, can you please share a code snippet if possible please :) – pavan teja Aug 29 '18 at 10:41

1 Answers1

1

Every time you restart the app adds a new "notify me in 24 h" event is added to the alarmmanager. so starting the app today 3 times you will get 3 alarms tomorrow.

  • Do you have code to ask the alarmmanager if there is already a "notify me in 24 h"?
  • Do you have code to remove an existing "notify me in 24 h"?
k3b
  • 14,517
  • 7
  • 53
  • 85