2012-10-25 12 views
7

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

public class MyBroadcastReceiver extends BroadcastReceiver { 

       private static final String TAG = "MyBroadcastReceiver"; 

       @Override 
       public void onReceive(Context context, Intent intent) { 
        String action = intent.getAction(); 
        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { 
         Uri data = intent.getData(); 
         //some code goes here 
         //get the id of the notification to cancel in some way 
         notificationhelper._completeNotificationManager.cancel(id);  
         } 
       } 
      } 

जहां

public class notificationhelper { 
    public static NotificationManager _completeNotificationManager = null; 

    public void complete() {   
     if (_completeNotificationManager == null) 
      _completeNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(
       R.drawable.notification, 
       _context.getString(R.string.notification), 
       System.currentTimeMillis()); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.flags |= Notification.FLAG_NO_CLEAR; 
     _completeNotificationManager.notify(TEXT, id, notification); 
    } 
} 

लेकिन notificationhelper._completeNotificationManager.cancel(id) काम नहीं करता: क्या मैं कर रहा हूँ निम्नलिखित है। मैंने notificationhelper._completeNotificationManager.cancelAll(); का उपयोग करने की कोशिश की और यह काम करता है। क्या मैं गलत हूं?

उत्तर

9

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

यदि NotificationManager.cancel() काम नहीं कर रहा है, तो अधिसूचना के लिए आईडी को बदलने का प्रयास करें।

notificationManager.notify(NOTIFICATION_ID, notification); 

जब मैं [RANDOM_NUMBER] 1 से NOTIFICATION_ID बदल गया है, यह जादुई काम करना शुरू किया। मुझे लगता है कि 1 किसी भी तरह, आरक्षित है हालांकि वहां किसी भी दस्तावेज में कोई टिप्पणी है ...

निश्चित रूप से एक आप रद्द करने के लिए एक ही NOTIFICATION_ID उपयोग सुनिश्चित करें:

notificationManager.cancel(NOTIFICATION_ID); 
+0

मैं एक ही समस्या के आसपास अपने सिर खरोंच कर रहा था। stackoverflow.com/questions/38880330 मेरा दिन बचाया। – mauron85

+0

क्या आप पूरा कोड अपलोड कर सकते हैं मेरे पास एक ही समस्या है? –

+0

मैं 7 अंकों की यादृच्छिक संख्या उत्पन्न करता हूं, अभी भी अधिसूचना कॉन्सेल काम नहीं कर रहा है:/ –

15

मेरे अनुभव में, आप कर सकते हैं ' टैग की परवाह किए बिना, किसी विशेष आईडी के साथ सभी नोटिफिकेशन रद्द करें।

है यही कारण है, अगर आप दो सूचनाएं ताकि तरह बनाने के लिए:

notificationManager.notify(TAG_ONE, SAME_ID, notification_one); 
notificationManager.notify(TAG_TWO, SAME_ID, notification_two); 
फिर

, notificationManager.cancel(SAME_ID) उनमें से या तो रद्द नहीं होगा! मुझे संदेह है कि ऐसा इसलिए है क्योंकि "टैग" फ़ील्ड, अगर अधिसूचित() और रद्द() में निर्दिष्ट नहीं है, तो शून्य पर डिफ़ॉल्ट है, जिसे आपको स्पष्ट रूप से रद्द करना है।

तो, इन दो सूचनाएं रद्द करने के लिए, आप कॉल करने के लिए है:

notificationManager.cancel(TAG_ONE, SAME_ID); 
notificationManager.cancel(TAG_TWO, SAME_ID); 

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

तो, या तो अपने टैग के रूप में पाठ प्रदान नहीं करतीं:

_completeNotificationManager.notify(id, notification); 

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

_completeNotificationManager.notify(TEXT, id, notification); 
collectionOfActiveTags.add(TEXT); 

... 

for (String activeTag : collectionOfActiveTags)  
    notificationhelper._completeNotificationManager.cancel(activeTag, id); 

मैं चाहता हूं कि आप जो करने का प्रयास कर रहे हैं वह समर्थित था, जैसा कि ऐसा लगता है कि यह होना चाहिए।

+0

("आप किसी विशेष आईडी के साथ सभी अधिसूचनाओं को रद्द नहीं कर सकते हैं") नोट ... आप कर सकते हैं, क्योंकि प्रत्येक नोटिफिकेशन के लिए एक आईडी के साथ "सभी नोटिस को एकजुट टैग" करने की आवश्यकता है। – delive

0

मुझे हाल ही में एक ही समस्या का सामना करना पड़ रहा था। मैंने इसे हल करने में कामयाब रहा है।

तो जो मैंने समझा।

1) आईडी का उपयोग करें जो मूल रूप से एक यादृच्छिक संख्या को सूचित करने और कोड के टुकड़े (रेसीवर/गतिविधि ...) पर भेजता है जहां आप इसे रद्द करना चाहते हैं।

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

तो अंतिम जवाब ... क्या मैं का इस्तेमाल किया है और क्या मेरे लिए काम करता है:

NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext()); 

notificationCompat.cancel(getIntent().getIntExtra("notif_id")); 
:

STEP 1: 
int notif_id = (int)(System.currentTimeMillis()%10000); 

STEP2: add this id inside the action intent (I am launching an activity where the notification gets cancelled on the action click): 

       Intent notificationSettingsIntent = new Intent(context.getApplicationContext(), NotificationSettingsActivity.class); 
       notificationSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
       notificationSettingsIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
       notificationSettingsIntent.putExtra("fromNotification",true); 
       notificationSettingsIntent.putExtra("notif_id",notif_id); 
       PendingIntent notificationSettingsActivityPendingIntent = PendingIntent.getActivity(context,notif_id,notificationSettingsIntent,PendingIntent.FLAG_ONE_SHOT); 

STEP 3: notify using the id in the step 1 but with no tags 

NotificationManagerCompat notificationCompat = NotificationManagerCompat.from(context.getApplicationContext()); 

notificationCompat.notify(notif_id,notificationBuilder.build()); 

अब गतिविधि जो मेरे कार्रवाई क्लिक करके खोला जाता है में, मैं सूचना के रूप में रद्द

हर समय काम करता है।

0

मेरी सूचनाएं हटाई नहीं जा रही थी क्योंकि मेरी सेवा फोरग्राउंड सेवा थी और स्टार्ट सेवा द्वारा नियमित सेवा शुरू नहीं की गई थी।

यदि आपकी सेवा अग्रभूमि है, तो रोकें() के बजाय stopForeground(true) पर कॉल करें। तो अब मेरा कोड इस तरह दिखता है:

NotificationManagerCompat.from(this).cancel(NotificationHelper.PLAYER_NOTIFICATION_ID); 
stopForeground(true); 

और यह काम करता है, अधिसूचना हटा दी गई थी।

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