Xamarin Android Weekly重复警报没有解雇

问题描述 投票:0回答:1

我正在尝试安排每周通知。对于特定的日期和时间,每周重复但它不起作用。这是我安排警报的代码

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天。

xamarin xamarin.android alarmmanager android-alarms repeatingalarm
1个回答
0
投票

我认为问题出在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);

HIH

© www.soinside.com 2019 - 2024. All rights reserved.