Search code examples
androidalarmmanagerandroid-notificationsandroid-alarmsnotificationmanager

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,
                PendingIntent.FLAG_UPDATE_CURRENT));

the receiver:

public class AlertReceiver extends BroadcastReceiver {   
int idAplicacion;


@Override
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
            NotificationCompat.Builder(context)
            .setContentTitle(title)
            .setContentText(msgText)
            .setTicker(sticker)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource( context.getResources(), R.drawable.ic_launcher));


    mBuilder.setContentIntent(notificIntent);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
    mBuilder.setAutoCancel(true);

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NotificationID.getID(), mBuilder.build());

   }
}

and public class NotificationID {

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

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

manifest

     <uses-permissionandroid:name="com.android.alarm.permission.SET_ALARM"/>

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

Solution

  • 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);
        ...
    }