我正在尝试安排每周通知。对于特定的日期和时间,每周重复一次 但它不起作用。这是我调度警报的代码
var id = Convert.ToInt32(string.Format("{0}{1}{2}", task.Id, hour, minutes));
var intent = new Intent(context, typeof(AlarmReceiver));
intent.PutExtra(AndroidConstants.NotificationId, id);
intent.PutExtra(AndroidConstants.NotificationMessage, $"{context.Resources.GetString(Resource.String.general_push_reminder_body)} {task.Name}");
intent.PutExtra(AndroidConstants.NotificationTitle, context.Resources.GetString(Resource.String.general_push_reminder_title));
var pendingIntent = PendingIntent.GetBroadcast(context, id, intent, PendingIntentFlags.CancelCurrent);
Calendar calendar = Calendar.Instance;
calendar.Set(CalendarField.DayOfWeek, (int)day);
calendar.Set(CalendarField.HourOfDay, hour);
calendar.Set(CalendarField.Minute, minutes);
calendar.Set(CalendarField.Second, 0);
if (calendar.Before(Calendar.Instance))
{
Log.Info("Task", $"Adding 7 days as scheduled time is past");
calendar.Add(CalendarField.DayOfYear, 7);
}
var alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
alarmManager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, 7 * AlarmManager.IntervalDay, pendingIntent);
这是我的接收器
[BroadcastReceiver(Enabled = true)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
try
{
Log.Info("Task", $"Alarm manager received");
var title = intent.GetStringExtra(AndroidConstants.NotificationTitle);
var message = intent.GetStringExtra(AndroidConstants.NotificationMessage);
var id = intent.GetIntExtra(AndroidConstants.NotificationId, 0);
Log.Info("Task", $"Showing Notification with id {id} {title} and {message}");
NotificationHandler.ShowNotification(context, id, title, message);
}
catch (Exception ex)
{
Log.Error("Task", ex.ToString());
}
}
也不例外。我已经尝试了几种选择,但AlarmReceiver
根本没有触发。
如果我选择今天的一天并在当前时间之后给出时间,仍然会触发if block for adding time for past
并额外添加 7 天。
我认为问题出在alarmManager.SetRepeating(...)
.
如果你看到Android.App.AlarmManager.SetRepeating的文档,它指出第二个参数:
triggerAtMillis:警报应首先响起的时间(以毫秒为单位(,使用适当的时钟(取决于警报类型(。
您正在传递calendar.TimeInMillis
根据文档为您提供:
当前时间(以纪元的 UTC 毫秒为单位
(
这不是你想要的。
我宁愿不使用Calendar
而改用DateTime
,因为它更容易使用,并且可以在所需的日期时间和当前日期时间之间以毫秒为单位进行差异,即:
// here I hardcode the values but you should set whatever you want or get it from the user
DayOfWeek desiredDayOfWeek = DayOfWeek.Saturday;
int desiredHour = 1, desiredMinute = 30, desiredSecond = 0;
// Calculate which will be the desired date time in which the alarm will go off for the first time
var differenceInNumberOfDaysBetweenDayOfWeek = desiredDayOfWeek - DateTime.Today.DayOfWeek;
var nextDayOfWeek = differenceInNumberOfDaysBetweenDayOfWeek >= 0
? DateTime.Today.AddDays(differenceInNumberOfDaysBetweenDayOfWeek)
: DateTime.Today.AddDays(differenceInNumberOfDaysBetweenDayOfWeek + 7);
var desiredDateTime = new DateTime(nextDayOfWeek.Year, nextDayOfWeek.Month, nextDayOfWeek.Day, desiredHour, desiredMinute, desiredSecond);
// this are the milliseconds left to arrive to the desired date time for the alarm to go off
var desiredMillisToArriveToDateTime = (desiredDateTime - DateTime.Now).Milliseconds;
var alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
alarmManager.SetRepeating(AlarmType.RtcWakeup, desiredMillisToArriveToDateTime, 7 * AlarmManager.IntervalDay, pendingIntent);
嗨