Sunny
Sunny

Reputation: 219

Multiple notifications for one activity

Actually my application have one activity.For creating a notification i have to pass the pending activity intent.

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.It is simple class. Is it possible to create notification more than one in this case?and how? Thanks in advance!

Upvotes: 3

Views: 761

Answers (1)

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

To have separate notifications you need to use a different ID for each one.

this is a simple sample:

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

Upvotes: 8

Related Questions