Android Xamarin 通知生成器设置不工作时

Viv*_*vek 2 xamarin.android

我正在尝试在 Xamarin Forms 中实现本地推送通知。我正在使用 DependencyService 来获取通知的 Android 实现。

这是我用来在特定时间推送通知的代码:

public void SetNotification(DateTime notificationDate, TimeSpan notificationTime)
        {
            long dateInMilli = (long)(notificationDate.Add(notificationTime) - DateTime.MinValue).TotalMilliseconds;

            Notification.Builder builder = new Notification.Builder(Xamarin.Forms.Forms.Context)
                .SetContentTitle("Test Notification")
                .SetContentText("Notification from Xamarin Forms")
                .SetSmallIcon(Resource.Drawable.icon)
                .SetDefaults(NotificationDefaults.Sound)
                .SetWhen(dateInMilli);

            Notification notification = builder.Build();
            NotificationManager notificationManager =
                                        (NotificationManager)Forms.Context.GetSystemService(Context.NotificationService);
            const int notificationId = 0;
            notificationManager.Notify(notificationId, notification);
        }
Run Code Online (Sandbox Code Playgroud)

问题是,通知不是按照设置的日期和时间立即显示的SetWhen(dateInMilli)

Zro*_*roq 5

SetWhen用于显示您放入通知时间戳的时间,而不是在 X 之后发送通知。您必须为此使用警报。

请参阅此内容以了解该课程。