Home » Android » android – Firebase firing too many alarms

android – Firebase firing too many alarms

Posted by: admin May 14, 2020 Leave a comment

Questions:

I already have an alarm that fires daily which has FLAG_UPDATE_CURRENT. This is the log from developer’s console.

java.lang.SecurityException: [email protected] many alarms (500) registered from pid 32326 uid 10206
    at android.os.Parcel.readException(Parcel.java:1540)
    at android.os.Parcel.readException(Parcel.java:1493)
    at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:206)
    at android.app.AlarmManager.setImpl(AlarmManager.java:428)
    at android.app.AlarmManager.set(AlarmManager.java:215)
    at com.google.firebase.iid.FirebaseInstanceIdService.zzagl(Unknown Source)
    at com.google.firebase.iid.FirebaseInstanceIdService.zzd(Unknown Source)
    at com.google.firebase.iid.FirebaseInstanceIdService.zza(Unknown Source)
    at com.google.firebase.iid.FirebaseInstanceIdService.zzm(Unknown Source)
    at com.google.firebase.iid.zzb$2.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)

I am using Firebase 9.6.1.


UPDATE:

public static void setAlarm(Context context, boolean isSet) {
  try {
     AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     Intent intent = new Intent(context, AlarmReceiver.class);

     boolean isWorking = (PendingIntent.getBroadcast(context,
                SettingsActivity.REQUEST_CODE, intent, PendingIntent.FLAG_NO_CREATE) != null);

     if (isSet && !isWorking) {
        PendingIntent sender = PendingIntent.getBroadcast(context,
                                   SettingsActivity.REQUEST_CODE, intent, 
                                   PendingIntent.FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.HOUR, 10);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                               AlarmManager.INTERVAL_DAY, sender);
      } else {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                                      SettingsActivity.REQUEST_CODE, intent,
                                      PendingIntent.FLAG_CANCEL_CURRENT);
        am.cancel(pendingIntent);
        pendingIntent.cancel();
      }
  } catch (Exception e) {
      Crashlytics.logException(e);
  }
}
How to&Answers:

According to this thread, the bug has been fixed by the Firebase team in the 10.0 release. Release notes are here.