2012-09-23 19 views
8

अद्यतन करने के लिए कैसे करें हाय मैं एक ही दृश्य में सभी अधिसूचनाएं दिखाना चाहता हूं .. और स्टेटस बार में अधिसूचना की संख्या अपडेट करना चाहता हूं ... यह सभी जानकारी अपडेट कर रहा है लेकिन हमेशा संख्या दिखा रहा है .. कृपया मुझे बताएं कि कैसे इसे हल करने के लिए ...अधिसूचना संख्या

@Override 
public void onReceive(Context context, Intent intent) 
{ 
    //Random randGen = new Random(); 
    //int notify_id = randGen.nextInt(); 
    NotificationManager notificationManager = (NotificationManager) 
     context.getSystemService(Activity.NOTIFICATION_SERVICE); 
    String title = intent.getStringExtra(TableUtils.KEY_TITLE); 
    String occasion = intent.getStringExtra(TableUtils.KEY_OCCASION); 
    Notification notification = 
     new Notification(R.drawable.icon, "Love Cardz" , 
         System.currentTimeMillis()); 
    // notification.vibrate = new long[]{100,250,300,330,390,420,500}; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.number+=1; 
    Intent intent1 = new Intent(context, ThemesBrowserActivity.class); 
    PendingIntent activity = 
     PendingIntent.getActivity(context, 1 , intent1, 
            PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, occasion, title, activity); 
    notificationManager.notify(1, notification); 
} 

उत्तर

18

आपको गिनती का ट्रैक रखना होगा। आप आवेदन वर्ग का विस्तार कर सकते हैं:

public class AppName extends Application { 
    private static int pendingNotificationsCount = 0; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    public static int getPendingNotificationsCount() { 
     return pendingNotificationsCount; 
    } 

    public static void setPendingNotificationsCount(int pendingNotifications) { 
     pendingNotificationsCount = pendingNotifications; 
    } 
} 

और तुम onReceive को संशोधित करना चाहिए:

@Override 
public void onReceive(Context context, Intent intent) { 
    ... 
    int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1; 
    AppName.setPendingNotificationsCount(pendingNotificationsCount); 
    notification.number = pendingNotificationsCount; 
    ... 
} 

और तुम गिनती रीसेट सकता है जब उपयोगकर्ता अधिसूचना खोलें:

AppName.setPendingNotificationsCount(0); 
+12

सुंदर हास्यास्पद कैसे फ्रेमवर्क में एक सरल, 'getNotifications (int id)' कॉल नहीं है, बस इसे जांचने के लिए ... – clu

+5

दुर्भाग्य से यदि एप्लिकेशन मारे गए हैं, तो काउंटर रीसेट हो सकता है ... शायद सहेजना चाहिए दृढ़ता के लिए साझा प्रेरणा – xnagyg

+1

काउंटर को रीसेट करने के लिए अधिसूचना खोले जाने पर आप कैसे जान सकते हैं ...? – Micro

4

यह मेरा कोड है, और यह काम करता है। मैंने केवल पुराने एंड्रॉइड संस्करणों पर परीक्षण किया है। मुझे नए संस्करणों पर संदेह है कि "संख्या" बैज अदृश्य बना दिया गया है, लेकिन मुझे इसका परीक्षण करने का मौका नहीं मिला है।

void notifyMsgReceived(String senderName, int count) { 
    int titleResId; 
    String expandedText, sender; 

    // Get the sender for the ticker text 
    // i.e. "Message from <sender name>" 
    if (senderName != null && TextUtils.isGraphic(senderName)) { 
     sender = senderName; 
    } 
    else { 
     // Use "unknown" if the sender is unidentifiable. 
     sender = getString(R.string.unknown); 
    } 

    // Display the first line of the notification: 
    // 1 new message: call name 
    // more than 1 new message: <number of messages> + " new messages" 
    if (count == 1) { 
     titleResId = R.string.notif_oneMsgReceivedTitle; 
     expandedText = sender; 
    } 
    else { 
     titleResId = R.string.notif_missedCallsTitle; 
     expandedText = getString(R.string.notif_messagesReceivedTitle, count); 
    } 

    // Create the target intent 
    final Intent intent = new Intent(this, TargetActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    final PendingIntent pendingIntent = 
     PendingIntent.getActivity(this, REQUEST_CODE_MSG_RECEIVED, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Build the notification 
    Notification notif = new Notification(
     R.drawable.ic_notif_msg_received, // icon 
     getString(R.string.notif_msgReceivedTicker, sender), // tickerText 
     System.currentTimeMillis()); // when 
     notif.setLatestEventInfo(this, getText(titleResId), expandedText, pendingIntent); 
    notif.number = count; 
    notif.flags = Notification.FLAG_AUTO_CANCEL; 

    // Show the notification 
    mNotificationMgr.notify(NOTIF_MSG_RECEIVED, notif); 
} 

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

इसी प्रकार, संख्या बैज छुपाया नहीं जाएगा (संख्या, आप) यदि आप संख्या को 1 से कम संख्या में सेट करते हैं तो हो सकता है कि पुनः दिखाए जाने से पहले अधिसूचना को साफ़ करना इसे ठीक कर सकता है।

0

आपको गिनती का ट्रैक रखना होगा। Notif.number पर जो वृद्धि आप करने की कोशिश कर रहे हैं वह काम नहीं कर रहा है, क्योंकि वह राज्य अनुपलब्ध है (यानी notif.number हमेशा 0 है, तो आप इसे 1 तक बढ़ाते हैं)। (साझा वरीयताओं, शायद), और वेतन वृद्धि कहीं आपके एप्लिकेशन में संख्या पर नज़र रखें और यह वहाँ की दुकान है, तो जब आप अधिसूचना का निर्माण, सेट

notif.number = myStoredValueForWhatShouldShowInNumber; 

कि आज़मा कर देखें।

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