首页 > 解决方案 > 特定日期和月份的Android xamarin本地通知

问题描述

一段时间以来,他一直试图在应用程序中为给定的日期和月份设置通知。不幸的是,通知会在每天调用它几秒钟后出现。

这是我的代码:

private void Remind_Click(object sender, System.EventArgs e)
    {
        string title = "If you see this";
        string message = "it means it works";

        Intent alarmIntent = new Intent(Application.Context, typeof(AlarmReceiver));
        alarmIntent.PutExtra("message", message);
        alarmIntent.PutExtra("title", title);

        var pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
        var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();

        DateTime nowDate = DateTime.Now;
        int month = 09;
        int day = 24;
        DateTime newDate = new DateTime(nowDate.Year, month, day);                   

        DateTime date = newDate;
        var ms = (long)(date - new DateTime(1970, 1, 1)).TotalMilliseconds;
        alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, 3600000, ms, pendingIntent);

        ShowSnack(main, $"Set date: {date} ");
    }

请帮我解决这个问题。

标签: androidxamarinnotifications

解决方案


  1. 对于setInexactRepeating (int type, long startTime, long intervalTime, PendingIntent pi);第二个参数是开始执行时间,第三个参数是两次执行之间的间隔时间。所以你在输入参数时有错误。
  2. 如果你只触发一次事件,你可以使用set(int type, long startTime, PendingIntent pi);使用这种方法只会触发一次事件。

注意:因为您使用的是 AlarmType.RtcWakeup。DateTime.Now 的时间可能与 Utc 时间不同(可以使用 DateTime.Now 和 DateTime.UtcNow 来查看两者之间是否存在时间差)。


推荐阅读