Android: After creating a new notification, the older one is replaced

前端 未结 3 973
耶瑟儿~
耶瑟儿~ 2021-02-18 17:43

I want to create a notification without canceling/deleting previous notifications from my app. Here is my code for creating a notification:

private void notifica         


        
相关标签:
3条回答
  • 2021-02-18 18:31

    for long term solutions, you can use a random method to generate integer variable for you and call that in notify.

    mNotificationManager.notify(createRandomCode(7), mBuilder.build());
    
    public int createRandomCode(int codeLength) {
            char[] chars = "1234567890".toCharArray();
            StringBuilder sb = new StringBuilder();
            Random random = new SecureRandom();
            for (int i = 0; i < codeLength; i++) {
                char c = chars[random.nextInt(chars.length)];
                sb.append(c);
            }
            return Integer.parseInt(sb.toString());
        }
    
    0 讨论(0)
  • 2021-02-18 18:36

    You are using a hardcoded id for your notification, of course it will replace the old one. Try using a variable ID instead of a hardcoded 100.

    mNotificationManager.notify(100+x, mBuilder.build());
    

    or something of the sort.

    0 讨论(0)
  • 2021-02-18 18:40

    Use variable id for notification, I have faced this issue 1 Hour ago And used this technique to overcome this issue.

            val id= Random(System.currentTimeMillis()).nextInt(1000)
            mNotificationManager?.notify(id, mBuilder.build())
    
    0 讨论(0)
提交回复
热议问题