2014-01-14 52 views
3

其實我的應用程序有一個活動。對於創建通知我必須通過掛起的活動意圖。一個活動的多個通知

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

這裏SaleNotification.class不是activity.It是簡單的類。 在這種情況下是否可以創建多個通知?以及如何? 在此先感謝!

回答

8

要有單獨的通知,您需要爲每個通知使用不同的ID

這是一個簡單的示例:

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

然後

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

和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); 
} 
+2

感謝它的工作原理... – Sunny

+2

它總是很高興接受的答案:) –