2011-06-03 11 views
19

में एकाधिक स्टेटसबार नोटिफिकेशन कैसे बनाएं I एकाधिक स्टेटसबार नोटिफिकेशन बनाने की आवश्यकता है। जब मैं स्टेटसबार खींचता हूं, तो एक अधिसूचना आइकन को सूची के रूप में प्रदर्शित किया जाना चाहिए। प्रत्येक अधिसूचना आइकन को अगले पृष्ठ पर प्रदर्शित करने के लिए अलग डेटा दिखाना चाहिए। मैं यह कैसे कर सकता हूं?एंड्रॉइड

मेरे कोड:

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

उत्तर

29

आपको प्रत्येक अधिसूचना में एक अद्वितीय आईडी पास करने की आवश्यकता है। एक बार जब आप अधिसूचना पर क्लिक कर लेंगे तो आप इसे हटाने के लिए उस आईडी का उपयोग करेंगे।

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 - onCreate में()

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

उत्तर के लिए धन्यवाद .. सीएल क्या है? – sanjay

+0

मैं दो अधिसूचनाओं के लिए डेटा कैसे प्रदर्शित कर सकता हूं? – sanjay

+0

@rochdev आपका कोड सही है लेकिन मैं प्रत्येक अधिसूचना क्लिक पर अलग-अलग डेटा कैसे प्राप्त कर सकता हूं? मैंने कोशिश की। मुझे कई अधिसूचनाएं मिलेंगी, लेकिन प्रत्येक सूचना क्लिक पर वही इरादा डेटा भेज दिया जाएगा। मुझे प्रत्येक अधिसूचना पर अलग-अलग डेटा की आवश्यकता है सर्वर इसे एक पेलोड बंडल में भेज रहा है। मैं यह कैसे कर सकता हूं? –

7

बस mNotificationManager.notify(ID, notifyDetails);

पर अलग आईडी का उपयोग आप आईडी पुनः उपयोग करते हैं यह एक नया एक, बजाय पुराने अद्यतन किया जाएगा नहीं जोड़ा जाएगा।

notifications का उपयोग करने के तरीके पर एक मार्गदर्शिका दी गई है।

+0

डेटाबेस में _id की तरह होने के लिए मैं एक आईडी कैसे जोड़ूं? प्रत्येक नया एक +1? –

+0

मुद्दा यह है कि आपको विभिन्न आईडी द्वारा अधिसूचित करने की आवश्यकता है! – cwhsu

1

This example आप प्रत्येक सूचना पर विभिन्न डेटा दिखाना चाहते हैं तो कई अधिसूचना बनाने का तरीका

0

को दर्शाता है। अपने लंबित Intent में ध्वज FLAG_UPDATE_CURRENT का उपयोग करें।

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

यह हर अधिसूचना पर डेटा अद्यतन करता है, यह हर बार बनाए बिना।

2

आप notfication आईडी chnage करने के लिए है, क्योंकि यह हर समय नहीं समाधान है कि आप एक ही करने के लिए ramdom संख्या की अवधारणा

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

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^