Trombone0904
Trombone0904

Reputation: 4258

android multiple notifications with alarm manager

i have this code for set multiple notifications with the alarm manager:

Receiver

public class NotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent) {

        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        notificationManager.notify(intent.getIntExtra(NOTIFICATION_ID, 0), notification);

    }
}

Main

 private void scheduleNotification(Notification notification, int delay) {


        Intent notificationIntent = new Intent(this, NotificationPublisher.class);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, (int)System.currentTimeMillis());
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int)System.currentTimeMillis(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        long futureInMillis = System.currentTimeMillis() + delay;
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent);
    }

    private Notification getNotification(String content) {

        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Scheduled Notification");
        builder.setContentText(content);
        builder.setSmallIcon(R.drawable.ic_notification_appicon);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setDefaults(Notification.DEFAULT_VIBRATE);
        builder.setAutoCancel(true);
        builder.setLights(Color.BLUE, 500, 500);
        return builder.getNotification();
    }

in on create i put this code:

scheduleNotification(getNotification("3 second delay"), 3000);
scheduleNotification(getNotification("5 second delay"), 5000);

both notifications will be shown, but both to the same time (after 5 seconds) where is my mistake?

Upvotes: 0

Views: 1681

Answers (2)

Trombone0904
Trombone0904

Reputation: 4258

Okay, i found the solution!

i change:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int)System.currentTimeMillis(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)

to

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int)System.currentTimeMillis(), notificationIntent, PendingIntent.FLAG_ONE_SHOT);

Upvotes: 2

Saif
Saif

Reputation: 713

you have to change this line

notificationManager.notify(intent.getIntExtra(NOTIFICATION_ID, 0), notification);

Replace with this

notificationManager.notify(Unique_Integer_Number, notification);

Unique integer number means you have to set integer value that will never repeated. example 0,1,2,3,4,5,....!!!!

Upvotes: 1

Related Questions