Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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()); } 
like image 673
M.Veli Avatar asked Jun 26 '14 20:06

M.Veli


People also ask

How do I get old notifications back on my Android?

Enable and check notification history on AndroidSwipe up from the homescreen to open the app drawer menu. Find and open the Settings app (it looks like a gear icon). Open Notifications. Select Notification history.

Can you see your old com notifications Android?

You can see your Android notification history on Android 11 through the Settings app. You first need to turn on Notification history in your phone's notification settings. You will then be able to see all the alerts you dismissed in the past 24 hours.

What are my old notifications?

Pull down the Notification Shade twice, and tap the gear icon under the quick tiles section. Once you're in the Settings app, tap Notifications. In the resulting screen (Figure 1), tap Notifications. Accessing the Notification History in Android 12.


2 Answers

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.

like image 169
JoxTraex Avatar answered Dec 06 '22 13:12

JoxTraex


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()) 
like image 26
Mahmoud Mabrok Avatar answered Dec 06 '22 13:12

Mahmoud Mabrok