2012-11-30 15 views
16

मैं अधिसूचना बार पर कुछ अधिसूचना भेजता हूं, जब मैं अधिसूचना में से एक क्लिक करता हूं तो मैं इसे साफ़ करना चाहता था। अभी के लिए मैं ध्वज का उपयोग करके एक करके एक को साफ़ करता हूं। मुझे पता है कि notificationManager.cancelAll() सभी अधिसूचनाओं को साफ़ कर सकता है लेकिन मुझे कहां रखा जाना चाहिए ताकि अधिसूचना में से एक बार क्लिक हो जाने पर मैं ट्रिगर कर सकूं।एक बार क्लिक किए जाने पर सभी अधिसूचनाओं को सही तरीके से कैसे साफ़ किया जाए?

private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 
    String title = context.getString(R.string.app_name); 
    Intent notificationIntent = new Intent(context, MainActivity.class); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = 
      PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notificationManager.notify(msgid, notification); 
    //notificationManager.cancelAll(); //i wan to clear all when the notification is clicked, where should i put this line? 
} 

उत्तर

58

मेरा समाधान इसे onResume() पर कॉल करना है।

@Override 
protected void onResume() { 
super.onResume(); 

// Clear all notification 
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
nMgr.cancelAll(); 
} 
+0

यह मेरे मामले में काम नहीं कर रहा है। –

+0

मेरे लिए काम नहीं कर रहा है –

+0

आकर्षण की तरह काम कर रहा है –

1

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

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