28

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

private void notification(Context context, String title, String content) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle(title)
            .setContentText(content);

    Intent resultIntent = new Intent(context, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //Id allows you to update the notification later on.
    mNotificationManager.notify(100, mBuilder.build());
}
M.Veli
  • 519
  • 1
  • 6
  • 15

4 Answers4

39

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.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
7

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())
Mahmoud Mabrok
  • 1,362
  • 16
  • 24
1

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());
    }
subrata sharma
  • 344
  • 4
  • 17
0

While create the PendingIntent object we call the PendingIntent.getActivity(mContext, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

So here use the below code to generate the PendingIntent

Random objRandom = new Random();
    final PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext,
                    objRandom.nextInt(100),
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
Somnath
  • 159
  • 1
  • 1
  • 7