我正在使用 xamarin 表单,我遇到了这个插件 https://github.com/thudugala/Plugin.LocalNotification 以便向用户显示通知。但是如何在通知区域显示进度条呢? 我找不到如何完成此操作的示例。
这是我用来发送通知的代码
CrossLocalNotifications.Current.Show("title", "body");
有如上例所示的跨平台解决方案吗?或者使用依赖服务的正确实现?
本地通知插件无法实现您想要实现的目标。然而,扩展库来为进度条上显示的进度数据添加额外的参数应该是相当简单的。
SetProgress(int max, int progress, bool intermediate)
方法从通知生成器传递两个附加值。 Google 的 Android 文档对此有最好的解释这里。
您应该添加对
SetProgress()
的调用的方法是 Android 特定类 /Platform/Droid/NotificationServiceImpl.cs中的
ShowNow()
。当然,您还需要在其他地方进行更改,以便可以从跨平台代码提供 max 和 progress 值。
如果上述解决方案看起来太复杂,并且您没有广泛使用该插件,也许您可以在 Android 项目中自己构建通知并使用依赖项服务执行该代码。
编辑:我删除了 Google 图书链接,该链接似乎并不适合所有人。相反,here 是 Microsoft Docs 中的一篇文章,详细介绍了如何创建本地通知。 guies 中唯一缺少的额外内容是显示进度条所需的 SetProgress 方法。
另外请注意,您需要一次又一次地提交通知才能在进度栏中显示进度。查看 Xamarin 论坛中此线程的第三条回复(来自 Cheesebaron),以获取有关其工作原理的简短说明和代码。
youtube 上有一个使用此插件的视频https://github.com/thudugala/Plugin.LocalNotification
这是链接 https://www.youtube.com/watch?v=5aIh-UFP2mA&t=2s https://github.com/DevHN504/ProgressBarPlugin/blob/main/ProgressBarPlugin/MainPage.xaml.cs
这是代码
private static int _progress;
private async void ShowNotification(object sender, EventArgs e)
{
_progress = 0;
var notificationRequest = new NotificationRequest
{
NotificationId = new Random().Next(),
Title = "Progress Bar",
Description = $"{_progress}%",
Android = new Plugin.LocalNotification.AndroidOption.AndroidOptions()
{
ProgressBarMax = 100,
ProgressBarProgress = 0,
IsProgressBarIndeterminate = false
}
};
await LocalNotificationCenter.Current.Show(notificationRequest);
Device.StartTimer(TimeSpan.FromSeconds(3), () =>
{
_progress += 10;
notificationRequest.Android.ProgressBarProgress = _progress;
notificationRequest.Description = $"{_progress}%";
LocalNotificationCenter.Current.Show(notificationRequest);
if (_progress == 100)
{
return false;
}
return true;
});
}