19

I need to create multiple statusbar notifications. When i pull down the statusbar, multiple notification icons should be displayed as a list. Each notification icon should show separate data to display on next page.How could i do this?

My code:

public class SimpleNotification extends Activity {

private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;

String str="Hai";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());


    Button start = (Button)findViewById(R.id.notifyButton);
    Button cancel = (Button)findViewById(R.id.cancelButton);



        start.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {


            Context context = getApplicationContext();
            CharSequence contentTitle = "Notification Details...";
            CharSequence contentText = "Browse Android Official Site by clicking me";
            Intent notifyIntent = new Intent(SimpleNotification.this,
                    sub.class);

            Bundle bundle = new Bundle();
            bundle.putString("welcome",str);
            notifyIntent.putExtras(bundle);

            PendingIntent intent = 
                PendingIntent.getActivity(SimpleNotification.this, 0, 
                notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

            notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
            mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
        }
    });

Here I did for one notification, but I need to create multiple notifications that each notification should show each data.

Nanne
  • 64,065
  • 16
  • 119
  • 163
sanjay
  • 2,590
  • 17
  • 55
  • 88
  • Possible duplicate of [Multiple notifications to the same activity](http://stackoverflow.com/questions/6066363/multiple-notifications-to-the-same-activity) – Balu SKT Mar 08 '16 at 07:24

5 Answers5

30

You need to pass a unique ID to each notification. Once you have clicked on the notification you use that ID to remove it.

public class SimpleNotification extends Activity {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Button start = (Button) findViewById(R.id.start_button);        

        start.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // 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);
            }
        });
    }

    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);
    }
}

MyActivityA - in onCreate()

...
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID_A);
...
rochdev
  • 3,835
  • 2
  • 21
  • 18
  • How can i display data for two notifications? – sanjay Jun 03 '11 at 08:55
  • @rochdev Your code is right but how can I get different data on each notification click?I tried it.I will get multiple notifications but the same intent data is send on each notification click.I need different data on each notification click as what server is sending it in a payload bundle.How can I do this? – Nitish Patel Oct 19 '13 at 06:18
  • @rochdev this will create new notification what if I want to update the previous with latest message and old – ingsaurabh Feb 26 '15 at 11:51
7

Just use different IDs on mNotificationManager.notify(ID, notifyDetails);

If you reuse the ID it will not add a new one, instead the old will be updated.

Here is a guide on how to use notifications.

H9kDroid
  • 1,814
  • 15
  • 17
2

you have to chnage the notfication id because it's all time not the solution is that you have to use ramdom number concept

Random random = new Random();
int randomNumber = random.nextInt(9999 - 1000) + 1000;
notificationManager.notify(randomNumber, notification);
RaviPatidar
  • 1,438
  • 1
  • 18
  • 29
1

This example shows how to create multiple Notification

egrunin
  • 24,650
  • 8
  • 50
  • 93
Sanath
  • 493
  • 7
  • 22
0

If you want to show different data on each notification. Use the Flag FLAG_UPDATE_CURRENT in your pending Intent.

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

It updates the data on every notification, without having to recreate it each time.

edwoollard
  • 12,245
  • 6
  • 43
  • 74
g4gaj
  • 85
  • 2
  • 11