1

Hi I have created an Notification class from which I can create multiple notification like this:

int id=0;
id++;
notification = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setSmallIcon(icon)
            .setContentText(dataNotes)
            .setWhen(time)
            .setAutoCancel(false)
            .setOngoing(true)
            .addAction(action)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(dataNotes))
            .build();

          nm.notify(id,notification);

So, my question is how can I get id of each notification that i create.I want to use that id when i want to cancel a particular notification. I know StatusBarNotification contains getId()method to get id but i don't know how to implement it.Can anyone help me.

Adarsh
  • 165
  • 2
  • 16
  • Consider storing the ids when you create them. Then you have a huge list of notification ids you can use as you wish. Could be better ways, I'm new to notification usage. – IAmGroot May 09 '17 at 09:46
  • @DoomsKnight hi i have tried that method by storing all id in a arraylist but further i am not getting any idea on how to use it – Adarsh May 09 '17 at 09:49
  • If you have the id of the one you wish to cancel, you can call the `cancel` method on the notification manager, using that id. See answer here: http://stackoverflow.com/a/3595241/940834 – IAmGroot May 09 '17 at 13:14

1 Answers1

2

Simply declare your notification Id as static. Every time you create a notification a unique Id will be set corresponding to each notification.

This will fetch you the intent id every time you perform a action on the notification.

Create a notification button as follows to perform some action on each notification.

public ActionButtonConfig getEditCityButton(CountryConfig countryConfig,
  CityConfig cityConfig, Integer notificationId) {
ArrayList<Class> params = new ArrayList<>();
params.add(context.getClass());
params.add(countryConfig.getClass());
params.add(cityConfig.getClass());
params.add(notificationId.getClass());

ArrayList<Object> arguments = new ArrayList<>();
arguments.add(context);
arguments.add(countryConfig);
arguments.add(cityConfig);
arguments.add(notificationId);
return getActionButton(NotificationButton.ACTION, getParamArray(params),
    arguments.toArray());

}

This Action Button Config will provide you enabling any action you need to perform on the notification.

public static int notificationCount = 0;

      void buildNotification(NotificationConfig notification) {
        try {

  buildNotification(//get items from notification config here and also notificationCount);
          notificationCount++;
        } catch (Exception e) {
          Log.e("Notification builder err",e);
        }
      }

In the above code see that notification count is static and every time a notification is generated, notification count is updated and previous value can be used at later stage henceforth.

Further notifying using Notification Manager to generate the notification as done by you in the last line.

Hope this works. Cheers

Prashast
  • 198
  • 1
  • 2
  • 12