我正在尝试在 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)
。
归档时间: |
|
查看次数: |
860 次 |
最近记录: |