我正在尝试使用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触发
什么可能导致这种行为?如何避免此意外通知,而是仅在更新时间触发。
我每次更新通知时间时都试图取消AlarmManager。仍未按预期运作。
[更新1]:我卸载了应用程序并再次运行它。一旦我设置了时间并保存通知,我立即收到应用程序通知。 (时间比当前时间提前15小时)
您可以通过重复警报来轻松解决此问题。将上次处理警报的时间写入共享首选项。当你醒来时,检查一下这个偏好。如果不到一天,则返回并且不处理此警报。