警报管理器时间更新后触发错误

时间:2018-09-13 17:50:47

标签: android alarmmanager android-notifications

我正在尝试使用Alarm Manager进行重复通知。

我有一个随时间选择器打开的活动,用户可以使用应用程序的默认时间或自定义选择时间并设置通知。完成后,将运行以下代码来设置警报触发器。

        notificationTime = new Session(context).getNotificationTime();
        if(notificationTime == null){
            // Set App default time (16:30)
            notificationTime = new NotificationTime();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, notificationTime.hours);
        calendar.set(Calendar.MINUTE, notificationTime.minutes);
        calendar.set(Calendar.SECOND, 0);
        Intent intent = new Intent(context, NotificationReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);

        if(Build.VERSION.SDK_INT < 19){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }else{
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }

警报触发器调用我在其中生成通知的NotificationReceiver BrodcastReceiver

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder;

        String name = "Reminder";
        String id = "Reminder"; // The user-visible name of the channel.
        String description = "Reminder"; // The user-visible description of the channel.

        Intent repeatingIntent = new Intent(context, RepeatingActivity.class);
            repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Log.d("My Log", "Broadcast Received");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notificationManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, name, importance);
                mChannel.setDescription(description);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notificationManager.createNotificationChannel(mChannel);
            }
            builder = new NotificationCompat.Builder(context, id);

            builder.setContentTitle("Custom Alarm")  // required
                    .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
                    .setContentText("Reminder")  // required
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setTicker("Custom Alarm")
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        }else{
            builder = new NotificationCompat.Builder(context);

            builder.setContentTitle("Custom Alarm")                           // required
                    .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
                    .setContentText("Reminder")  // required
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .setTicker("Custom Alarm")
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                    .setPriority(Notification.PRIORITY_HIGH);

        }
        Log.d("My Log", "Notification Triggered");
        Notification notification = builder.build();
        notificationManager.notify(100, notification);

除了以下情况外,此设置可以正常运行: 1.假设我将通知设置为在13:00触发

  1. 通知将按预期触发

  2. 现在在几分钟之内,如果我转到应用程序并将通知时间设置为20:00

  3. 我立即收到通知(即,我将时间更改为20:00并保存了通知时间)。在LogCat部分中还记录了“已收到广播”和“已触发通知”日志。

什么可能导致此行为?如何避免出现此意外通知,而仅在更新时触发它。

每次通知时间更新时,我都试图取消AlarmManager。仍然无法正常运行。

[更新1]:我卸载了该应用,然后再次运行。设置时间并保存通知后,我立即收到了应用程序通知。 (时间设置为比当前时间早15小时)

1 个答案:

答案 0 :(得分:0)

您可以通过消除警报来简单地解决此问题。将上次处理警报的时间记录为共享首选项。醒来后,请检查该首选项。如果不到一天,请返回并且不处理此警报。