【发布时间】:2018-09-14 01:56:10
【问题描述】:
我正在尝试使用警报管理器进行重复通知。
我有一个使用时间选择器打开的活动,用户可以使用应用默认时间或自定义选择时间并设置通知。完成后,将运行以下代码来设置警报触发器。
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 触发
通知按预期触发
现在几分钟内,如果我去应用程序并将通知时间设置为 20:00
我会立即收到通知(即,只要我将时间更改为 20:00 并保存通知时间)。 LogCat 部分也记录了“已收到广播”和“已触发通知”的日志。
什么可能导致这种行为?如何避免此意外通知,而是仅在更新时间触发它。
每次更新通知时间时,我都尝试取消 AlarmManager。仍然无法按预期运行。
[更新 1]:我卸载了该应用程序并再次运行它。设置时间并保存通知后,我立即收到了 App 通知。 (时间设置为比当前时间提前 15 小时)
【问题讨论】:
标签: android alarmmanager android-notifications