简体   繁体   中英

Multiple AlarmManager firing isn't firing at a time

When I set multiple AlarmManager at the same time, it only fires the first one.

There is a MessageReceiver class that extends BroadcastReceiver. When AlarmManager will fire MessageReceiver (onReceive() method) will be executed. From MainActivity, I create PendingIntent and set the AlarmManager.

MessageReceiver.java

public class MessageReceiver extends BroadcastReceiver {
@Override
    public void onReceive(Context context, Intent intent) {
        Log.d("Msg","triggered");
        //Do some work
    }
 }

MainActivity.java

AlarmManager msgAlarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
Intent msgIntent = new Intent(MainActivity.this,MessageReceiver.class);


PendingIntent msgPending = PendingIntent.getBroadcast(context,
    ++requestCode,
    msgIntent,
    PendingIntent.FLAG_ONE_SHOT);//requestCode is a global variable


msgAlarmManager.setExact(
        AlarmManager.RTC_WAKEUP,
        calender.getTimeInMillis(),
        msgPending); //calender is a Calender Object

This piece of code from MainActivity.java will be executed multiple times in a single runtime of my application.

I want every single Alarm to be fired even if they supposed to be fired at the same time. But in my case, the only first one is firing and another AlarmManager isn't. How can I make sure that all AlarmManager is firing?

I have sold this issue before like this :

private void createReminder(Activity activity, Notification notification, MyModelClass model) {
    final int min = 1;
    final int max = 9999;
    final int random = new Random().nextInt((max - min) + 1) + min;
    Intent notificationIntent = new Intent(activity, AlarmReceiver.class);
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, random, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    long delay = model.dateAndTime.getTime() - Calendar.getInstance().getTimeInMillis();
    long futureInMillis = SystemClock.elapsedRealtime() + delay;
    AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null && model.isAlarmOn) {
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
    }
}

private Notification getNotification(Activity activity, MyModelClass model) {
    PendingIntent newEntryActivityPendingIntent = PendingIntent.getActivity(activity, 1, new Intent(activity, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(activity, Common.CHANNEL_ID)
            .setContentTitle("Content title")
            .setContentText("ContentText")
            .setTicker("Hey Come on !")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true)
            .setContentIntent(newEntryActivityPendingIntent);
    return builder.build();
}

And my receiver like this ;

   public class AlarmReceiver extends BroadcastReceiver {
    public static final String NOTIFICATION = "notification";

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        notificationManager.notify(1, notification);
    }

}

Dont forget to add channel id for sdk>26

public class MyApplication extends MultiDexApplication {

public static final String CHANNEL_ID = "reminderNotificationChannel"
     private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Reminder Notification Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            if (manager != null) {
                manager.createNotificationChannel(serviceChannel);
            }

        }
    }
}

As you know, random is a changing request code for each alarms. By doing this, we are getting all different alarms.

Note : Please add MyApplication class to your manifest file!

Happy Codes!

Simply just using a unique value as request code helped to solve the problem. So, the change will be only into the PendingIntent and updated PendingIntent will be like this:

Random random = new Random();
PendingIntent msgPending = PendingIntent.getBroadcast(context,
random.nextInt(10000),
msgIntent,
PendingIntent.FLAG_ONE_SHOT);

Now multiple alarm will be fired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM