Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Multiple statusbar Notifications in android

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.

like image 988
sanjay Avatar asked Jun 03 '11 07:06

sanjay


People also ask

How do I show multiple notifications on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

How can Android handle multiple push notifications?

If you have multiple push providers you will need create your own Messaging Service to handle push notifications. You will need to pass new tokens to Swrve and make sure Swrve is set to handle incoming notifications.

How do I show status bar notifications?

By default, if you see a notification icon in the notification bar that you want to see more about, swipe down once from the top of your screen to expand your notifications.


2 Answers

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);
...
like image 95
rochdev Avatar answered Oct 06 '22 01:10

rochdev


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.

like image 25
H9kDroid Avatar answered Oct 05 '22 23:10

H9kDroid