Reputation: 31
I am using xamarin forms and i came across this plugin https://github.com/thudugala/Plugin.LocalNotification in order to show notifications to user. But how can a progress bar be shown on notification area? I cant find an example of how this is done.
This is the code i use for sending notification
CrossLocalNotifications.Current.Show("title", "body");
Is there a cross platform solution as the above example? Or a correct implementation using Dependency Services?
Upvotes: 2
Views: 1087
Reputation: 1
There's a video on youtube that uses this plugin https://github.com/thudugala/Plugin.LocalNotification
here is the link https://www.youtube.com/watch?v=5aIh-UFP2mA&t=2s https://github.com/DevHN504/ProgressBarPlugin/blob/main/ProgressBarPlugin/MainPage.xaml.cs
this is the code
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;
});
}
Upvotes: 0
Reputation: 7189
What you're trying to achieve is not possible with the local notification plugin. However, it should be rather trivial to extend the library to add an additional argument for the progress data shown on the progress bar.
Basically, you just need to pass two additional values from the Notification Builder by calling the SetProgress(int max, int progress, bool intermediate)
method. This is best explained in Google's Android documentation here.
The method were you should add the call to SetProgress()
is ShowNow()
in the Android specific class /Platform/Droid/NotificationServiceImpl.cs. Of course you also need to make changes elsewhere so that you can provide the max and progress values from the cross-platform code.
If the above solution seems too complex and you're not using the plugin extensively, perhaps you can just construct the notifications yourself in the Android project and execute that code using the dependency service.
Edit: I removed the Google Books link which doesn't seem to work for everyone. Instead, here is an article from Microsoft Docs, detailing how the local notifications can be created. The only additional thing that is missing from the guies is the SetProgress method which is required to show the progress bar.
Also, notice that you need to submit the notification again and again to show progress in the progress bar. Check the third reply (from Cheesebaron) on this thread in the Xamarin Forums for a short explanation and bits of code on how it works.
Upvotes: 1