MaxFraire MaxFraire - 2 years ago 181
Android Question

How to set multiple notifications using AlarmManager in Android

I have a list with items, within each item the user can set a notification for it, and then open it from the notification. It works fine but I can only set one notification, it doesn't matter if it's from the same item or another one. if I set more than one, it will only show the last one. I dont know what else I can do. Here is my code

inside the item-detail class

Long alertTime = new GregorianCalendar().getTimeInMillis() + 7 * 1000;

Intent alertIntent = new Intent(getApplicationContext(), AlertReceiver.class);
alertIntent.putExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(getApplicationContext(), 1, alertIntent,

the receiver:

public class AlertReceiver extends BroadcastReceiver {
int idAplicacion;

public void onReceive(Context context, Intent intent) {

idAplicacion = intent.getIntExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);

Log.e("TESTING", "the id is " + String.valueOf(idAplicacion));
createNotification(context, "some title", "some message text", "some sticker");

public void createNotification(Context context, String title, String msgText, String sticker)

Intent i = new Intent(context, DetalleAplicacionActivity.class);
i.putExtra(DataBaseManager.CN_ID_APLICACION, idAplicacion);

PendingIntent notificIntent = PendingIntent.getActivity(context,0, i ,PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder mBuilder = new
.setLargeIcon(BitmapFactory.decodeResource( context.getResources(), R.drawable.ic_launcher));


NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


public class NotificationID {

private final static AtomicInteger c = new AtomicInteger(0);

public static int getID() {
return c.incrementAndGet();



<receiver android:name=".Clases.AlertReceiver"/>

Answer Source

The reason is that you create notifications with the same requestCode - so they all are threated by Android as the same notification, you just update it.

Use some variety while creating notifications

public void createNotification(Context context, String title, String msgText, String sticker) {
    PendingIntent notificIntent = PendingIntent.getActivity(context,
            java.util.Random.nextInt(100000), i ,PendingIntent.FLAG_CANCEL_CURRENT);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download