2011-06-03 53 views
19

創建多個狀態欄通知我需要創建多個狀態欄通知。當我拉下狀態欄時,多個通知圖標應顯示爲列表。每個通知圖標應顯示單獨的數據以顯示在下一頁上。我可以如何做到這一點?如何在android系統

我的代碼:

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

在這裏,我做了一個通知,但我需要創建多個通知,每個通知應該顯示的每個數據。

+0

可能的[多個通知,重複相同的活動](http://stackoverflow.com/questions/6066363/multiple-notifications-to-the-same-activity) – 2016-03-08 07:24:38

回答

29

你需要一個唯一的ID傳遞給每一個通知。點擊通知後,您可以使用該ID將其刪除。

​​

MyActivityA - 中的onCreate()

... 
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID_A); 
... 
+0

感謝您的回覆..什麼是cls? – sanjay 2011-06-03 08:49:18

+0

如何顯示兩個通知的數據? – sanjay 2011-06-03 08:55:20

+0

@rochdev你的代碼是正確的,但我怎麼能得到每個通知點擊不同的數據?我想它。我會得到多個通知,但目的相同的數據發送每個通知click.I需要不同的數據每個通知點擊的是什麼服務器正在將它發送到一個有效載荷包中。我可以如何做到這一點? – 2013-10-19 06:18:09

7

mNotificationManager.notify(ID, notifyDetails);

只要使用不同的ID如果重用ID不會添加一個新的,而不是舊的將被更新。

下面是關於如何使用notifications指南。

+0

如何添加一個編號,以便像在數據庫中的_id?每個新的+1? – 2012-10-15 17:45:30

+0

問題是你需要通過不同的ID通知! – cwhsu 2014-10-29 09:36:22

0

如果你想顯示在每個通知不同的數據。使用標誌FLAG_UPDATE_CURRENT在待定Intent

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

它更新每個通知的數據,而不必每次都重新創建它。

2

你必須chnage的notfication ID,因爲它所有的時間不是解決的辦法是,你必須使用ramdom數概念

Random random = new Random(); 
int randomNumber = random.nextInt(9999 - 1000) + 1000; 
notificationManager.notify(randomNumber, notification);