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
You just need to change your one-line from notificationManager.notify(0, notification);
to notificationManager.notify((int) System.currentTimeMillis(), notification);
...
This will change the id of notification whenever the new notification will appear
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")
You need to add a unique ID to each of the notifications so that they do not combine with each other. You can use this link for your reference :
https://github.com/sanathe06/AndroidGuide/tree/master/ExampleCompatNotificationBuilder
just replace your line with this
notificationManager.notify(Unique_Integer_Number, notification);
hope it will help you.
I solved my problem like this...
/**
* 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());
}
Using Shared Preferences worked for me
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();