2010-09-02 7 views
36

मैंने एक ऐसा ऐप बनाया है जो एंड्रॉइड फोन के ड्रॉप-डाउन स्टेटस बार में नोटिफिकेशन सेट करता है। हालांकि, मेरे कोड में एक बग है (कभी-कभी नोटिफिकेशन सेट होते हैं, कभी-कभी वे नहीं होते हैं)। मैं जांच करने में सक्षम होना चाहता हूं (कोड में) अगर अधिसूचना उपयोगकर्ता के लिए दृश्यमान है। (यानी उपयोगकर्ता स्टेटस बार में अधिसूचना देख सकता है?)।एंड्रॉइड देव में स्टेटस बार में कौन सी नोटिफिकेशन सक्रिय हैं यह जांचने के लिए कैसे?

मैं यह कैसे कर सकता हूं? (अग्रिम में धन्यवाद)।

नमूना कोड की बहुत सराहना की जाती है।

उत्तर

36

मैं जांच (कोड में) में सक्षम होना चाहता हूं अगर अधिसूचना उपयोगकर्ता के लिए दृश्यमान है। (यानी उपयोगकर्ता स्टेटस बार में अधिसूचना देख सकता है?)।

मैं यह कैसे कर सकता हूं?

आप क्षमा नहीं कर सकते हैं। अपडेट: अब Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

हालांकि साथ संभव है, आप हमेशा बस cancel() यह कर सकते हैं - नहीं परदे पर एक Notification रद्द कि बिल्कुल ठीक है। इसके विपरीत, आप को उसी Notification के लिए सुरक्षित रूप से सुरक्षित रूप से कॉल कर सकते हैं, और Notification पहले से ही स्क्रीन पर होने पर भी कोई समस्या नहीं आएगी।

संपादित करें:

NotificationManager.getActiveNotifications() एपीआई 23 में जोड़ा गया आप NotificationListenerService

+0

यदि आप फिर से सूचित करते हैं तो – weakwire

+0

@weakwire को अधिसूचित करते हैं, तो अधिसूचना के लिए यह फिर से लिखना होगा: केवल अगर अधिसूचना पहले से स्क्रीन पर नहीं थी। – CommonsWare

+0

उसे नहीं पता था। सवाल का जवाब नहीं दिया है लेकिन कोड की 2 लाइनों के साथ उद्देश्य प्रदान करता है। +1 – weakwire

24

आपको अपनी प्रत्येक सूचना के लिए एक आईडी सेट करने की आवश्यकता है।

ताकि आप एक अधिसूचना बनाने ..

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0); 

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent); 

Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis()); 
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent); 
manger.notify(notId, notification); 

यह स्पष्ट करने के लिए ..

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0); 
pendingIntent.cancel(); 

और अगर सक्रिय .. जाँच करने के लिए (existAlarm रिटर्न अशक्त अगर कोई लंबित आशय उपलब्ध है)

public PendingIntent existAlarm(int id) { 
    Intent intent = new Intent(this, alarmreceiver.class); 
    intent.setAction(Intent.ACTION_VIEW); 
    PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE); 
    return test; 
} 

तो प्रत्येक अधिसूचना के लिए एक आईडी शुरू करने के लिए सबकुछ नीचे आता है और आप इसे कैसे अद्वितीय बनाते हैं।

+9

@CommonsWare: अगर मैं लंबित इरादे से अधिसूचना को कॉल करें, मैं जांच सकता हूं कि लंबित इरादा मौजूद है या नहीं, अधिसूचना भी मौजूद है – weakwire

+0

आह, मैं देखता हूं कि आप कहां जा रहे हैं।दिलचस्प तकनीक - मैंने इस उद्देश्य के लिए इसे कहीं भी नहीं देखा है या इसे कहीं और नहीं देखा है। – CommonsWare

+1

लंबे समय ... लेकिन फिर भी .. क्या यह पोस्ट जीबी संस्करणों पर काम करता है? और यदि मैं टास्कस्टैकबिल्डर का उपयोग कर अधिसूचनाएं बना रहा हूं तो मैं इस स्निपेट का उपयोग कैसे कर सकता हूं? –

2

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

Notification notification = new Notification(icon, tickerText, when); 
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE; 

FLAG_ONLY_ALERT_ONCE:

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

हालांकि

, अधिसूचना होगा झपकी जब यह फिर से भेज दिया जाता है, लेकिन वहाँ किसी भी ध्वनि या कंपन नहीं होगा।

25

बस सब एक साथ रखा है। यह है कि यह कैसे काम करता है

एक अधिसूचना निर्माण करने के लिए है,

 Notification n = new Notification.Builder(MyService.this) 
       .setContentTitle("Notification Title") 
       .setContentText("Notification Message") 
       .setSmallIcon(R.drawable.myicon).build(); 

कोई नोटिफिकेशन ध्वनि कॉल अधिसूचना के setSound(),

 Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     Notification n = new Notification.Builder(MyService.this) 
       .setContentTitle("Notification Title") 
       .setContentText("Notification Message") 
       .setSound(alarmSound) 
       .setSmallIcon(R.drawable.myicon).build(); 

बनाने के अधिसूचना रद्द करने के लिए उपयोगकर्ता का चयन किया और रिसीवर आशय का शुभारंभ करने के बाद , setAutoCancel() फोन,

 Notification n = new Notification.Builder(MyService.this) 
       .setContentTitle("Notification Title") 
       .setContentText("Notification Message") 
       .setSound(alarmSound) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.myicon).build(); 

किसी विशेष अधिसूचना के लिए केवल एक बार ध्वनि/कंपन बनाने के लिए Notification.FLAG_ONLY_ALERT_ONCE का उपयोग करें। इस ध्वज के साथ, आपकी अधिसूचना केवल एक बार जब तक रद्द हो जाती है तब तक ध्वनि उत्पन्न हो जाएगी और आप अधिसूचना आईडी के साथ जितनी बार चाहें अधिसूचित() को कॉल कर सकते हैं। ध्यान दें कि यदि आप रद्द() को कॉल करते हैं या यदि उपयोगकर्ता ने अधिसूचना रद्द कर दी है या ऑटो रद्द कर दिया है, तो सूचित करें() कॉल अधिसूचना ध्वनि फिर से कर देगा।

 n.flags |= Notification.FLAG_ONLY_ALERT_ONCE; // Dont vibrate or make notification sound 

अंत में अधिसूचना पैनल पर अधिसूचना डाल करने के लिए,

 NotificationManager notificationManager = 
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     notificationManager.notify(notification_id, n); 

ध्यान दें कि यहां notification_id महत्वपूर्ण है कि आप प्रभावी रूप से अधिसूचना उपयोग करना चाहते हैं। (एक अधिसूचना के लिए या करने के लिए एक ध्वनि/कंपन रखने के लिए एक विशिष्ट अधिसूचना रद्द करें)।

एक विशेष अधिसूचना को रद्द करने के

 NotificationManager notificationManager = 
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notificationManager.cancel(notification_id); 

आप cancel() कर सकते हैं एक अधिसूचना भले ही वह मौजूद नहीं है या के रूप में आप एक ही आईडी वाले चाहते हैं आप notify() रूप में कई बार फोन कर सकते हैं। ध्यान दें कि विभिन्न आईडी के साथ अधिसूचना कॉल करने से नई अधिसूचनाएं बन जाएंगी।

तो, की अधिसूचना मौजूद है कि क्या हो या नहीं, अगर आप सही notification_idNotification.FLAG_ONLY_ALERT_ONCE ध्वज सेट के साथ के साथ फिर से notify() कहते हैं, तो आपको अपनी सूचना जिंदा दोहराया ध्वनियों के साथ उपयोगकर्ता परेशान किए बिना रख सकते हैं।

+0

'.setOnlyAlertOnce (true)' अधिसूचना कॉम्पेट – behelit

6

एक नई विधि एपीआई 23 में NotificationManager वर्ग के लिए शुरू की है:

public StatusBarNotification[] getActiveNotifications() 
+52

के लिए मैं 201 9 में इसका उपयोग करने के लिए तत्पर हूं। – ZaBlanc

+0

क्या यह एपीआई 18 से उपलब्ध नहीं है? Http: //developer.android.com/reference/android/service/notification/NotificationListenerService। एचटीएमएल # getActiveNotifications() – acrespo

+0

मैंने एंड्रॉइड.एप.NotificationManager :: getActiveNotifications और NotificationListenerService के बारे में लिखा था। –

0

एंड्रॉयड Marshmallow (एपीआई 23) के रूप में, आप सक्रिय नोटिफिकेशन आपके ऐप द्वारा पोस्ट की एक सूची ठीक हो सकता है। यह अधिसूचना प्रबंधक विधि getActiveNotifications() है। अधिक यहाँ जानकारी: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()

1

यह seems कि एंड्रॉयड एम (एपीआई 23) से यह NotificationListenerService का उपयोग कर और न ही अतिरिक्त अनुमति की आवश्यकता के बिना, इस तरह अपनी प्रक्रिया पाने के लिए संभव है:

notificationManager.getActiveNotifications()

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