2012-09-29 14 views
24

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

समस्या यह है कि मूल रद्द होने पर मैं एक नया नहीं उठाना चाहता हूं। क्या यह बताने का कोई तरीका है कि कोई सूचना दिखाई दे या रद्द हो गई हो? या केवल एक अधिसूचना अद्यतन करने का एक तरीका अगर यह मौजूद है?

उत्तर

29

यह मैं कैसे इसे हल है:

असल में, आप अपनी अधिसूचना के साथ एक इरादा बनाते हैं जो IntentService (या कोई अन्य सेवा) शुरू करता है और onHandleIntent में आप सेट कर सकते हैं एक ध्वज यह इंगित करता है कि अधिसूचना सक्रिय है या नहीं।
उपयोगकर्ता इस अधिसूचना को निकाल सकता है जब उपयोगकर्ता अधिसूचना (contentIntent) और/या जब उपयोगकर्ता सूची से इसे साफ़ करता है (deleteIntent)।

इसे स्पष्ट करने के लिए, मैं अपने स्वयं के ऐप में यही करता हूं। जब अधिसूचना के निर्माण मैं

Intent intent = new Intent(this, CleanupIntentService.class); 
Notification n = NotificationCompat.Builder(context).setContentIntent(
     PendingIntent.getActivity(this, 0, intent, 0)).build(); 

सेट करते हैं अधिसूचना उपयोग किया जाता है, मेरी CleanupIntentService शुरू की है, (जो सूचना बनाई सेवा में) एक ध्वज की स्थापना:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onCreate(); // If removed, onHandleIntent is not called 
    return super.onStartCommand(intent, flags, startId); 
} 


@Override 
protected void onHandleIntent(Intent intent) { 
    OtherService.setNotificationFlag(false); 
} 
+0

वहाँ कुछ क्या गलत है रहना होगा। जब मैं अपने '' ''रिज्यूमे()' '' विधि में लिखता हूं, तो '' '' (नोटिफिकेशन विज़िबल()) '' '' '' '(' अधिसूचना प्रबंधक) प्राप्त करें सिस्टम सेवा (NOTIFICATION_SERVICE)) रद्द करें (MY_ID); '' 'और उसके बाद' '' खत्म करें(); startActivity (getIntent()); '' 'मुझे एक लूप मिलता है जो मेरे फोन को पुनरारंभ करने की ओर जाता है। तो इस विधि का सावधानी से इलाज किया जाना चाहिए .... – maysi

+5

मेरे लिए काम नहीं कर रहा है। यह हमेशा मेरे लिए सच देता है। – Saket

+1

एंड्रॉइड 4.3 में मेरे लिए काम नहीं किया। अधिसूचना को खारिज करने के बाद 'लंबित इंटेन्टेंट' अटक गया, और पूरे ऐप को मारने के बाद भी। केवल फोन को पुनरारंभ करना इससे छुटकारा पा लिया। साथ ही, 'setLatestEventInfo() 'अब मौजूद नहीं प्रतीत होता है, इसलिए मैंने इसके बजाय' setContentIntent()' का उपयोग किया। – Sam

1

मुझे लगता है कि आप कक्षा के deleteIntent का उपयोग कर सकते हैं।

मुझे अपने एक आवेदन में याद है, जब मैं अधिसूचना रद्द कर दी जाती है या अधिसूचना ट्रे को मंजूरी दे दी जाती है तो मैं ब्रॉडकास्ट (कस्टम ब्रॉडकास्ट) को आग लगाने के लिए उपयोग का उपयोग करता हूं।

/** 
* Issues a notification to inform the user that server has sent a message. 
*/ 
private void generateNotification(String text) { 

    int icon = R.drawable.notifiaction_icon; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, text, 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, MY_ID, notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, text, intent); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; //PendingIntent.FLAG_ONE_SHOT 

    notificationManager.notify(MY_ID, notification); 
} 
1

deleteIntent के लिए एक वैकल्पिक जिसके बाद मेरे अपने अनुप्रयोग में फायदेमंद साबित हुआ है:

private boolean isNotificationVisible() { 
    Intent notificationIntent = new Intent(context, MainActivity.class); 
    PendingIntent test = PendingIntent.getActivity(context, MY_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE); 
    return test != null; 
} 

यह कैसे मैं अधिसूचना उत्पन्न है:

6

सलाम। आप API >= 23 उपयोग कर रहे हैं इस विधि का उपयोग कर सकते हैं सक्रिय अधिसूचना प्राप्त करने के लिए:

StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications(); 
for (StatusBarNotification notification : notifications) { 
    if (notification.getId() == 100) { 
    // Do something. 
    } 
} 
संबंधित मुद्दे

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