Multiple notifications for one action

In fact, my application has one activity . To create a notification, I must pass the intention of the pending activity.

NotificationManager mgr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Notification note=new Notification(mob.app.R.drawable.message,"Message!",System.currentTimeMillis());
        // This pending intent will open after notification click
        PendingIntent i=PendingIntent.getActivity(this, 2,new Intent(this,SaleNotification.class),0);


        note.setLatestEventInfo(activity,messageHeading,message, i);

        //After uncomment this line you will see number of notification arrived
        note.number=notifyNumber;
        mgr.notify(0, note);

Here SaleNotification.class is not an activity. This is a simple class. Is it possible to create a notification of more than one in this case? And How? Thanks in advance!

+3
source share
1 answer

To have separate notifications, you need to use a different one IDfor each of them.

this is a simple example:

private int SIMPLE_NOTFICATION_ID_A = 0;
private int SIMPLE_NOTFICATION_ID_B = 1;

then

// display A
displayNotification("Extra for A", "This is A", "Some text for activity A", MyActivityA.class, SIMPLE_NOTFICATION_ID_A);
// display B
displayNotification("Extra for B", "This is B", "Some text for activity B", MyActivityB.class, SIMPLE_NOTFICATION_ID_B);

and displayNotification:

private void displayNotification(String extra, String contentTitle, String contentText, Class<?> cls, int id) {     

  Notification notifyDetails = new Notification(R.drawable.icon, "New Alert!", System.currentTimeMillis());
  Intent intent = new Intent(this, cls);
  intent.putExtra("extra", extra);
  PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), id, intent, PendingIntent.FLAG_ONE_SHOT);
  notifyDetails.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
  mNotificationManager.notify(id, notifyDetails);
}
+8
source

All Articles