2013-06-17 5 views
8

मैं अपने ऐप पर पुश अधिसूचना का उपयोग कर रहा हूं। जब कोई पुश अधिसूचना आती है, तो मैं एक अधिसूचना प्रदर्शित करता था, अगर मैं एक और अधिसूचना भेजता हूं (पिछली अधिसूचना को साफ़ किए बिना) यह नई अधिसूचना को प्रतिस्थापित करता है।धक्का अधिसूचना प्राप्त करते समय एक नई अधिसूचना जोड़ें (पुराने को प्रतिस्थापित नहीं करें)

यह मैं

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      int icon = R.drawable.ic_launcher; 
      CharSequence tickerText = "New notification Pending"; 
      long time = System.currentTimeMillis(); 

      Notification notification = new Notification(icon, tickerText, time); 
      notification.flags = Notification.DEFAULT_LIGHTS 
        | Notification.FLAG_AUTO_CANCEL; 

      // Context context = getApplicationContext(); 
      CharSequence contentTitle = "Notifications"; 
      CharSequence contentText = newMessage; 
      Intent notificationIntent = new Intent(this, LoginActivity.class); 
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
        notificationIntent, 0); 
      notification.setLatestEventInfo(context, contentTitle, contentText, 
        contentIntent); 
      mNotificationManager.notify(1, notification); 

का उपयोग, लेकिन मैं अधिसूचना को बदलने के लिए नहीं करना चाहती कोड है, मैं एक नई अधिसूचना के रूप में यह जोड़ना चाहते थे।

उत्तर

9

आपको हर बार अधिसूचना आईडी के रूप में एक अलग आईडी की आपूर्ति करने की आवश्यकता है। सबसे अच्छा तरीका जीसीएम को एक आईडी फ़ील्ड भेजना होगा जिसे उसके जीसीएमआईएनएन्टेंट सेवा के मैसेज() विधि में Intent.getExtras().getInt() के माध्यम से एक्सेस किया जा सकता है।

यदि यह संभव नहीं है, तो मैं आपकी सूचना आईडी के रूप में एक यादृच्छिक पूर्णांक संख्या उत्पन्न करने के लिए (int)Math.random()*10 जैसे कुछ का उपयोग करने का सुझाव दूंगा। यह (आंशिक रूप से) सुनिश्चित करेगा कि आपकी सूचनाएं एक-दूसरे को प्रतिस्थापित नहीं करेंगी।

2

सरल आपके पास करने के लिए

परिवर्तन अधिसूचना आईडी

mNotificationManager.notify(1, notification); 

बजाय 1

के लिए

अधिक के बजाय एक नई अधिसूचना आईडी हर बार उल्लेख इस Link

2

उपयोग 1 को हार्डकोड की :

int i = x; //Generate a new integer everytime 
mNotificationManager.notify(i, notification); 
19

आप अपनी अधिसूचना में अद्वितीय आईडी असाइन करने के लिए System.currentTimeMillis() का भी उपयोग कर सकते हैं।

int id = (int) System.currentTimeMillis(); 
mNotificationManager.notify(id, notification); 
+1

इसका एक उत्कृष्ट विचार। क्योंकि समय आईडी कभी दोहराया नहीं जाता है। मैं PHP में भी इसका उपयोग करता हूं। धन्यवाद। लेकिन सूचित करने के लिए अपना कोड बदलें (आईडी, अधिसूचना); यह दूसरों के लिए उपयोगी हो सकता है –

+0

धन्यवाद इंद्र का उल्लेख करने के लिए, अब मैंने आईडी के साथ 1 को बदल दिया है। –

+0

thanqu sir ..for इसे पोस्ट किया गया –

2

मुझे यकीन है कि क्या आपके उपयोग के मामले है नहीं हूँ, लेकिन the Android design guidelines recommend not doing it at all.

मत करो: है:

+1

पूरी तरह से उपयोग के मामले पर निर्भर करता है, और अब अधिसूचना चैनलों के लिए धन्यवाद, कोई इस बारे में और अधिक स्पष्ट हो सकता है –

1

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

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

@param id An identifier for this notification unique within your application. 
    @param notification A {@link Notification} object describing what to show the user. 
    Must not be null. 

public void notify(int id, Notification notification) 
{ 
    notify(null, id, notification); 
} 

उदाहरण:

int id =(int) System.currentTimeMillis(); 
      mNotificationManager.notify(id, notify); 
संबंधित मुद्दे