2014-10-21 10 views
7

मुझे कोई समस्या है कि मुझे इसका जवाब नहीं मिल रहा है। मैंने एंड्रॉइड डेवलपर ट्यूटोरियल्स की कोशिश की है, मैंने यहां स्टैक ओवरफ्लो और Google पर खोज की है, लेकिन या तो मेरे खोज-कौशल भयभीत हैं या कोई जवाब नहीं है, मुझे लगता है कि मेरी समस्या का जवाब है।सेट समूह() का उपयोग कर किटकैट (एपीआई 1 9) में नोटिफिकेशन स्टैक करें

मैं एक से अधिक होने पर सभी नए संदेशों के लिए संदेश अधिसूचनाओं को एक अधिसूचना में ढेर करना चाहता हूं। मैं प्रत्येक संदेश के लिए एक अधिसूचना प्रकट कर सकता हूं लेकिन मैं एक स्टैक/सारांश अधिसूचना नहीं बना सकता। http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png

यह क्या मैं चाहता हूँ है:

सबसे अच्छा मैं

यह है http://developer.android.com/images/android-5.0/notifications/Summarise_Do.png

मैं एपीआई 19 का उपयोग कर रहा है और इसे वापस संगत होना जरूरी नहीं है। AndroidStudio में मेरा कोड राइटिग्न करें और जेनमोशन में परीक्षण करें।

मैं कई सूचनाएं होने का नतीजा साथ NotificationCompatManager उपयोग करने की कोशिश की है:

NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

मेरी सूचना दिखता है: NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

मैं भी एक ही परिणाम के साथ NotificationManager उपयोग करने की कोशिश की है, जो एक से अधिक सूचनाएं है इस तरह:

Notification notification = new NotificationCompat.Builder(this) .setContentTitle("New message from... ") .setContentText("Message:...") .setSmallIcon(R.drawable.icon) .setContentIntent(pIntent) .setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION)) .setGroup(mNoti) .setGroupSummary(true) .setAutoCancel(true) .build();

और मैं इस तरह अधिसूचना आग:

notificationManager.notify(counter, notification); 

कहाँ प्रत्येक अधिसूचना के लिए काउंटर में वृद्धि इसलिए प्रत्येक अधिसूचना का अपना आईडी है।

मैं भी succsess बिना निर्माण और अधिसूचना fiering के लिए इस प्रपत्र की कोशिश की है:

NotificationCompat.Builder notificationBuilder = 
      new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.icon) 
        .setContentTitle("message from...") 
        .setContentText("message...") 
        .setContentIntent(pIntent) 
        .setAutoCancel(true) 
        .setGroup(mNoti) 
        .setGroupSummary(true) 
        .setSound(RingtoneManager.getDefaultUri(TYPE_NOTIFICATION)); 


    notificationManager.notify(counter, notificationBuilder.build()); 

setGroup स्पष्ट रूप से मेरे लिए ध्यान देने योग्य बात है। मुझे समझ में नहीं आता कि यह क्यों काम नहीं करता है या मैं इसे कैसे काम करना चाहता हूं।

इस तरह अपने समूह देखो:

final static String mNoti = "mNoti"; 

केवल बात यह है कि दूर से जो मैं चाहता सभी सूचनाओं के लिए एक ही आईडी का उपयोग कर रहा है ताकि नई सूचनाएं पुराने लोगों को ओवरराइड करता है, और setNumber का उपयोग करने के करीब आता है (int) अधिसूचना का निर्माण करते समय। हालांकि यह मुझे सचमुच परिणाम नहीं देता है क्योंकि मुझे नहीं लगता कि मुझे उपयोगकर्ता द्वारा नियंत्रित अधिसूचनाओं की संख्या प्राप्त नहीं हो सकती है। या मैं कर सकता हूँ

क्या कोई मुझे मेरे वांछित लक्ष्य पर पहुंचने में मदद कर सकता है?

उत्तर

1

http://developer.android.com/images/android-5.0/notifications/Summarise_Dont.png में परिणाम मिलने के कारण यह है कि आप अपनी सभी सूचनाओं पर समूह सारांश सेट करते हैं। एपीआई दस्तावेज़ों में इसे वर्णित किया गया है: Set this notification to be the group summary for a group of notifications। इसका मतलब है कि प्रत्येक अधिसूचना को सारांश के रूप में माना जाएगा, और इस प्रकार एक नई अधिसूचना के रूप में प्रदर्शित किया जाएगा।

अपनी समस्या का समाधान करने के लिए, हर बार जब आप एक नई अधिसूचना प्राप्त करते हैं तो आप एक नया सारांश भेज सकते हैं। ध्यान दें कि मुझे 100% यकीन नहीं है कि यह सर्वोत्तम समाधान है, लेकिन यह समस्या हल करता है। मैं एक परीक्षण आवेदन जो बटन क्लिक के आधार पर सूचनाएं कहते हैं बनाया:

public class MyActivity extends Activity { 

    private final static String GROUP_KEY_MESSAGES = "group_key_messages"; 
    public static int notificationId = 0; 
    private NotificationManagerCompat manager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my); 

     manager = NotificationManagerCompat.from(this); 
     manager.cancelAll(); 
    } 

    public void addNotification(View v) { 
     notificationId++; // Increment ID 

     Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 

     Intent viewIntent = new Intent(this, ShowNotificationActivity.class); 
     viewIntent.putExtra("notificationId", notificationId); // Put the notificationId as a variable for debugging purposes 

     PendingIntent viewPendingIntent = PendingIntent.getActivity(this, notificationId, viewIntent, 0); // Intent to next activity 

     // Group notification that will be visible on the phone 
     Notification summary = new NotificationCompat.Builder(this) 
       .setAutoCancel(true) 
       .setContentIntent(viewPendingIntent) 
       .setContentTitle("New notifications") 
       .setContentText("You have "+notificationId+" new messages") 
       .setLargeIcon(icon) 
       .setGroup(GROUP_KEY_MESSAGES) 
       .setGroupSummary(true) 
       .build(); 

     manager.cancelAll(); // Is this really needed? 
     manager.notify(notificationId, summary); // Show this summary 
    } 
} 

ShowNotificationActivity:

public class ShowNotificationActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_notification); 

     Intent intent = getIntent(); 
     int counter = intent.getIntExtra("COUNTER", -57); 
     Log.d("COUNTER", ""+counter); 

     TextView view = (TextView)(findViewById(R.id.textView)); 
     view.setText("You have just read "+counter+" messages"); 
     MyActivity.notificationId = 0; // Reset ID. Should not be done like this, but used to show functionality. 
    } 
} 

पहली गतिविधि MyActivity अधिसूचना को संभालने के लिए प्रयोग किया जाता है। मुझे लगता है कि यह ज्यादातर रिसीवर में किया जाता है हालांकि। MyActivity में एक बटन addNotification को ट्रिगर करता है जो नए अधिसूचना सारांश को धक्का देता है। सारांश जोड़ने से पहले सभी पुराने सारांश रद्द कर दिए गए हैं। सारांश पर क्लिक करते समय आप स्टार्टअप गतिविधि पर जाते हैं, जिसे ShowNotificationActivty कहा जाता है जहां डेटा दिखाया जाता है। उदाहरण के लिए, यह एक ईमेल ऐप था, MyActivity कुछ प्रकार का ईमेल रिसीवर होगा जो ईमेल को डेटाबेस में सहेजता था। अधिसूचना आईडी को डेटाबेस में सहेजी गई कुछ आईडी पर सेट किया जा सकता है। जब सूचनाएं क्लिक की जाती हैं, तो ईमेल को अधिसूचना आईडी के आधार पर देखा जा सकता है।

मुझे आशा है कि इस मदद करता है :)

+1

मैं आपकी सलाह का पालन किया और setGroupSummary और setGroup का इस्तेमाल किया और यह काम किया है, केवल Android पर 5.0 और उच्चतर यद्यपि। 4.4 और 4.2 प्रत्येक अधिसूचना को अलग से दिखाएं। यह यहां मूल प्रश्न से परे है, इसलिए मैंने एक नया बनाया: http://stackoverflow.com/q/31483772/1204377 –

+0

@ एंटन चेरकाशिन क्या आप वाकई इस पंक्ति को शामिल कर चुके हैं: 'manager.cancelAll(); // क्या यह वास्तव में जरूरी है? पुरानी अधिसूचनाओं को केवल सारांश खोलने के लिए यह "चाल" है। – Pphoenix

+0

मैंने कोशिश की और फिर मुझे केवल फोन पर सारांश अधिसूचना दिखाई दे रही है, लेकिन मेरा अंतिम लक्ष्य एंड्रॉइड वेयर पर अधिसूचना छोड़ना है, और ऐसा करने के लिए मुझे एन व्यक्तिगत सूचनाएं + 1 सारांश अधिसूचना प्रति एंड्रॉइड डेवलपर जीवाइडलाइन : https://developer.android.com/training/wearables/notifications/stacks.html। कॉलिंग प्रबंधक.cancelAll(); एंड्रॉइड पहनने पर नोटिफिकेशन भी खारिज कर दिया। इसलिए यह काम नहीं करेगा, क्योंकि उन व्यक्तिगत अधिसूचनाओं में कस्टम क्रियाएं/पृष्ठ/आदि हैं –

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