How to display multiple notifications in android

前端 未结 18 1258
既然无缘
既然无缘 2020-11-29 19:39

I am receiving only one notification and if there comes another notification, it replaces the previous one and here is my code

private static void generateNo         


        
相关标签:
18条回答
  • 2020-11-29 20:18

    Another way of doing it is take the current date convert it into long just take the last 4 digits. There is a high probability that the number will be unique.

        long time = new Date().getTime();
        String tmpStr = String.valueOf(time);
        String last4Str = tmpStr.substring(tmpStr.length() -5);
        int notificationId = Integer.valueOf(last4Str);
    
    0 讨论(0)
  • 2020-11-29 20:19
    notificationManager.notify(0, notification);
    

    Put this code instead of 0

    new Random().nextInt() 
    

    Like below it works for me

    notificationManager.notify(new Random().nextInt(), notification);
    
    0 讨论(0)
  • 2020-11-29 20:21

    For Kotlin.

     notificationManager.notify(Calendar.getInstance().timeInMillis.toInt(),notificationBuilder.build())
    
    0 讨论(0)
  • 2020-11-29 20:23

    At the place of uniqueIntNo put unique integer number like this:

    mNotificationManager.notify(uniqueIntNo, builder.build());
    
    0 讨论(0)
  • 2020-11-29 20:24

    Below is the code for pass unique notification id:

    //"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.
    

    Reset notificationId in savedPreferences at specific range like I have did it at 1000. So it will not create any issues in future. Let me know if you need more detail information or any query. :)

    0 讨论(0)
  • 2020-11-29 20:26

    A simple counter may solve your problem.

    private Integer notificationId = 0;
    
    private Integer incrementNotificationId() {
       return notificationId++;
    }
    
    NotificationManager.notify(incrementNotificationId, notification);
    
    0 讨论(0)
提交回复
热议问题