【问题标题】:Android xamarin local notification in specific day and month特定日期和月份的Android xamarin本地通知
【发布时间】:2021-09-24 17:06:56
【问题描述】:

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

这是我的代码:

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} ");
    }

请帮我解决这个问题。

【问题讨论】:

    标签: android xamarin notifications


    【解决方案1】:
    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 来查看两者之间是否存在时差)。

    【讨论】:

    • 谢谢,成功了。
    猜你喜欢
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2012-04-13
    • 2017-01-19
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多