एंड्रॉइड में कई सूचनाएं कैसे प्रदर्शित करें


103

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

private static void generateNotification(Context context, String message,
        String key) {
    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,
            FragmentOpenActivity.class);
    notificationIntent.putExtra(key, key);
    // 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;

    notification.defaults |= Notification.DEFAULT_SOUND;

    // notification.sound = Uri.parse("android.resource://" +
    // context.getPackageName() + "your_sound_file_name.mp3");
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

}

3
आधिकारिक दस्तावेज़ के अनुसार, आपको सभी सूचनाओं को ढेर करने के लिए एक ऐप से कई सूचनाएं नहीं दिखानी चाहिए .. एक नज़र रखें: developer.android.com/design/patterns/notifications_k.html
गौतम कुमार

जवाबों:


134

बस इसके साथ अपनी लाइन बदलें

 notificationManager.notify(Unique_Integer_Number, notification);

आशा है इससे आपकी मदद होगी।


2
Unique_Integer_Numberआपके कोड में क्या है .. और किस कोड को बदलना चाहिए
कार्तिके '

4
विशिष्ट पूर्णांक संख्या का मतलब है कि आपको पूर्णांक मान सेट करना होगा जो कभी दोहराया नहीं जाएगा। उदाहरण 0,1,2,3,4,5, .... !!!!
साकेत शाह Shah

2
notificationManager.notify (1, सूचना); अधिसूचना प्रबंधक। नोट (2, अधिसूचना);
संचित शाह Sank

1
अधिसूचना के आते ही वेतन वृद्धि कैसे होगी ??
मितेश शाह

21
अद्वितीय पूर्णांक उत्पन्न करना: (int) ((नई तिथि ()। getTime () / 1000L)% पूर्णांक। MAX_VILUE);
एंड्री कोवलचुक

87

सरल अधिसूचना_ को परिवर्तनशील होने की आवश्यकता है।

नोटिफिकेशन_ड के लिए बस रैंडम नंबर बनाएं।

    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;

या आप इस विधि का उपयोग रैंडोरेंज द्वारा बताए गए यादृच्छिक संख्या बनाने के लिए कर सकते हैं (यह कभी नहीं दोहराया जाएगा):

    int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

और यादृच्छिक संख्या उत्पन्न करने के लिए अधिसूचना आईडी के लिए पैरामीटर जोड़ने के लिए इस लाइन को बदलें

    notificationManager.notify(m, notification);

8
थोड़ा हैकी और संभावना में चलता है कि आप एक ही अधिसूचना आईडी के साथ समाप्त हो जाएंगे, लेकिन यह काम करता है अगर आपको वास्तव में जल्दी कुछ चाहिए।
मुहम्मद अब्दुल-रहीम

1
अगर मुझे यह सही दिखाई देता है, तो टियोरेंज से एपोरच केवल सेकंड के साथ काम करता है। इसलिए यदि आपके पास एक ही समय में कई सूचनाएं हैं, तो यह काम नहीं करेगा।
परीक्षण

1
@ टिकना सही है। यही कारण है कि मेरे पास एक 2 कदम है, एम + = random.nextInt (100) + 1; यह एक कदम अतिरिक्त हो सकता है लेकिन इसका रास्ता सुरक्षित है। मैंने नीलामी / बोली एप के अंतिम मिनटों में उपरोक्त विधि को विफल देखा। इसलिए मैंने सुरक्षा के लिए एक और लाइन जोड़ी!
user3833732

27

साझा प्राथमिकता का उपयोग करना मेरे लिए काम कर गया

SharedPreferences prefs = getSharedPreferences(Activity.class.getSimpleName(), Context.MODE_PRIVATE);
int notificationNumber = prefs.getInt("notificationNumber", 0);
...

notificationManager.notify(notificationNumber , notification);
SharedPreferences.Editor editor = prefs.edit();
notificationNumber++;
editor.putInt("notificationNumber", notificationNumber);
editor.commit();

5
यह एक काफी स्मार्ट तरीका है अगर आपको हर नोटिफिकेशन को ट्रैक करने की जरूरत है। संभवतया यहां के एक होशियार जवाब।
मुहम्मद अब्दुल-रहीम


8

मुझे लगता है कि यह किसी को मदद करेगा ..
नीचे दिए गए कोड में "not_nu" एक यादृच्छिक int है .. PendingIntent और Notification की एक ही ID है .. ताकि प्रत्येक सूचना पर क्लिक करने पर इरादा अलग-अलग गतिविधि को निर्देशित करेगा ..

private void sendNotification(String message,String title,JSONObject extras) throws JSONException {
   String id = extras.getString("actionParam");
    Log.e("gcm","id  = "+id);
    Intent intent = new Intent(this, OrderDetailActivty.class);
    intent.putExtra("id", id);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final int not_nu=generateRandom();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, not_nu /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_cart_red)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(not_nu /* ID of notification */, notificationBuilder.build());
}
public int generateRandom(){
    Random random = new Random();
    return random.nextInt(9999 - 1000) + 1000;
}

मेरी सूचनाएं अभी भी ढेर नहीं हो रही हैं, क्या कोई विशिष्ट चीजें हैं जो मैं यहां दिखाने के अलावा करता हूं?
सिंह 8989

क्या है कि random.nextInt गणना वहाँ कर ... आप समझा सकते हैं ??? 9999-1000 ???? वह क्या है ...
रादु

@Radu जैसा कि आप कोड में देख सकते हैं "notificationManager.notify (पहला पैरामीटर के लिए एक int (ID के लिए)) लेता है। यदि यह Int (ID) नए नोटिफिकेशन के लिए समान है तो यह पुराने को बदल देगा और नया दिखाता है। यदि यह Int (ID) अलग है, तो नई अधिसूचना अलग से व्यवहार की जाती है और स्टैक के रूप में दिखाई देती है। इसलिए इसे प्राप्त करने के लिए पुराना नोटिफिकेशन रहता है। हम यादृच्छिक int बना रहे हैं और ID के रूप में असाइन कर रहे हैं। "random.nextInt (9999 - 1000) + 1000; "इस कोड का उपयोग करते हुए।
मुनिफ एम

@ Lion789 आपको बस नए नोटिफिकेशन के लिए अलग-अलग आईडी का उपयोग करना होगा फिर उसे नोटिफिकेशन को स्टैक करना चाहिए।
मुनीफ एम

नया अधिसूचना.कॉम। Android Oreo में पदावनत किया गया है, कृपया डॉक्स जांचें और अधिसूचना चैनल कार्यान्वयन का उपयोग करें।
तपनएचपी

5

इस uniqueIntNoतरह से अद्वितीय पूर्णांक संख्या डालें:

mNotificationManager.notify(uniqueIntNo, builder.build());

4

आपको प्रत्येक सूचना के लिए एक अद्वितीय आईडी जोड़ने की आवश्यकता है ताकि वे एक दूसरे के साथ गठबंधन न करें। आप इस लिंक का उपयोग अपने संदर्भ के लिए कर सकते हैं:

https://github.com/sanathe06/AndroidGuide/tree/master/ExampleCompatNotificationBuilder


3

मैंने अपनी समस्या को इस तरह हल किया ...

/**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message,
            String keys, String msgId, String branchId) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationCompat.Builder nBuilder;
        Uri alarmSound = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        nBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Smart Share - " + keys)
                .setLights(Color.BLUE, 500, 500).setContentText(message)
                .setAutoCancel(true).setTicker("Notification from smartshare")
                .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
                .setSound(alarmSound);
        String consumerid = null;
        Integer position = null;
        Intent resultIntent = null;
        if (consumerid != null) {
            if (msgId != null && !msgId.equalsIgnoreCase("")) {
                if (key != null && key.equalsIgnoreCase("Yo! Matter")) {
                    ViewYoDataBase db_yo = new ViewYoDataBase(context);
                    position = db_yo.getPosition(msgId);
                    if (position != null) {
                        resultIntent = new Intent(context,
                                YoDetailActivity.class);
                        resultIntent.putExtra("id", Integer.parseInt(msgId));
                        resultIntent.putExtra("position", position);
                        resultIntent.putExtra("notRefresh", "notRefresh");
                    } else {
                        resultIntent = new Intent(context,
                                FragmentChangeActivity.class);
                        resultIntent.putExtra(key, key);
                    }
                } else if (key != null && key.equalsIgnoreCase("Message")) {
                    resultIntent = new Intent(context,
                            FragmentChangeActivity.class);
                    resultIntent.putExtra(key, key);
                }.
.
.
.
.
.
            } else {
                resultIntent = new Intent(context, FragmentChangeActivity.class);
                resultIntent.putExtra(key, key);
            }
        } else {
            resultIntent = new Intent(context, MainLoginSignUpActivity.class);
        }
        PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
                notify_no, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (notify_no < 9) {
            notify_no = notify_no + 1;
        } else {
            notify_no = 0;
        }
        nBuilder.setContentIntent(resultPendingIntent);
        NotificationManager nNotifyMgr = (NotificationManager) context
                .getSystemService(context.NOTIFICATION_SERVICE);
        nNotifyMgr.notify(notify_no + 2, nBuilder.build());
    }

3

इसे करने का एक और तरीका है कि वर्तमान तिथि को इसे लंबे समय में बदल दें और अंतिम 4 अंक ले लें। एक उच्च संभावना है कि संख्या अद्वितीय होगी।

    long time = new Date().getTime();
    String tmpStr = String.valueOf(time);
    String last4Str = tmpStr.substring(tmpStr.length() -5);
    int notificationId = Integer.valueOf(last4Str);

केवल अंतिम चार अंकों का उपयोग करें और स्वयं डेटाइम का क्यों नहीं?
मुहम्मद अब्दुल-रहीम

4
यहाँ थोड़ा छोटा कोड है:int notificationId = System.currentTimeMillis()%10000;
bvk256

केवल 4 अंक क्यों?
पावेल बिरुकोव

2

आपको बस अपनी एक-पंक्ति notificationManager.notify(0, notification);को notificationManager.notify((int) System.currentTimeMillis(), notification);... से बदलने की आवश्यकता है

जब भी नई अधिसूचना दिखाई देगी, यह अधिसूचना की आईडी को बदल देगा


1
notificationManager.notify(0, notification);

इस कोड को 0 के बजाय डालें

new Random().nextInt() 

जैसे नीचे यह मेरे लिए काम करता है

notificationManager.notify(new Random().nextInt(), notification);

1
समीक्षा से: हाय, कृपया स्रोत कोड के साथ जवाब न दें। आपका समाधान कैसे काम करता है, इसके बारे में अच्छा विवरण देने का प्रयास करें। देखें: मैं एक अच्छा उत्तर कैसे लिखूं? । धन्यवाद
sɐunıɔ ןɐ qɐp

0

समस्या आपके साथ है notificationId। इसे एक सरणी सूचकांक के रूप में सोचें। हर बार जब आप अपनी अधिसूचना को अपडेट करते हैं, तो notificationIdवह स्थान है जहां मूल्य को संग्रहीत करने के लिए इसे लिया जाता है। जैसा कि आप अपने अंतर मूल्य (इस मामले में, आपके notificationId) में वृद्धि नहीं कर रहे हैं , यह हमेशा पिछले एक को बदल देता है। सबसे अच्छा समाधान मुझे लगता है कि आप एक अधिसूचना को अपडेट करने के बाद इसे बढ़ाना चाहते हैं। और आप इसे लगातार रखना चाहते हैं, तो आप अपने का मूल्य स्टोर कर सकते हैं notificationIdमें sharedPreferences। जब भी आप वापस आते हैं, तो आप केवल पिछले पूर्णांक मान ( notificationIdसंग्रहीत sharedPreferences) में ले सकते हैं और इसका उपयोग कर सकते हैं।


0

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

//"CommonUtilities.getValudeFromOreference" is the method created by me to get value from savedPreferences.
String notificationId = CommonUtilities.getValueFromPreference(context, Global.NOTIFICATION_ID, "0");
int notificationIdinInt = Integer.parseInt(notificationId);

notificationManager.notify(notificationIdinInt, notification);

// will increment notification id for uniqueness
notificationIdinInt = notificationIdinInt + 1;
CommonUtilities.saveValueToPreference(context, Global.NOTIFICATION_ID, notificationIdinInt + "");
//Above "CommonUtilities.saveValueToPreference" is the method created by me to save new value in savePreferences.

विशिष्ट श्रेणी notificationIdमें रीसेट करें savedPreferencesजैसे मैंने 1000 पर किया है। इसलिए यह भविष्य में कोई समस्या पैदा नहीं करेगा। मुझे बताएं कि क्या आपको अधिक विस्तार से जानकारी या किसी प्रश्न की आवश्यकता है। :)


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

0

अपने कोड में निम्न विधि का उपयोग करें।

विधि कॉल: -

notificationManager.notify(getCurrentNotificationId(getApplicationContext()), notification);

तरीका:-

  *Returns a unique notification id.
         */

        public static int getCurrentNotificationId(Context iContext){

            NOTIFICATION_ID_UPPER_LIMIT = 30000; // Arbitrary number.

            NOTIFICATION_ID_LOWER_LIMIT = 0;
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(iContext);
        int previousTokenId= sharedPreferences.getInt("currentNotificationTokenId", 0);

        int currentTokenId= previousTokenId+1;

        SharedPreferences.Editor editor= sharedPreferences.edit();

        if(currentTokenId<NOTIFICATION_ID_UPPER_LIMIT) {

            editor.putInt("currentNotificationTokenId", currentTokenId); // }
        }else{
            //If reaches the limit reset to lower limit..
            editor.putInt("currentNotificationTokenId", NOTIFICATION_ID_LOWER_LIMIT);
        }

        editor.commit();

        return currentTokenId;
    }

-1

एक साधारण काउंटर आपकी समस्या को हल कर सकता है।

private Integer notificationId = 0;

private Integer incrementNotificationId() {
   return notificationId++;
}

NotificationManager.notify(incrementNotificationId, notification);


-1
val notifyIdLong = ((Date().time / 1000L) % Integer.MAX_VALUE)
var notifyIdInteger = notifyIdLong.toInt()
if (notifyIdInteger < 0) notifyIdInteger = -1  * notifyIdInteger // if it's -ve change to positive
notificationManager.notify(notifyIdInteger, mBuilder.build())
log.d(TAG,"notifyId = $notifyIdInteger")
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.