我正在尝试在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);
}
问题是,通知显示的时间不是SetWhen(dateInMilli)
设置的日期和时间。