Xamarin отображать уведомление в панели уведомлений после загрузки файла - PullRequest
0 голосов
/ 16 октября 2019

У меня есть этот код для загрузки файла, который работает правильно:

var base64EncodedBytes = System.Convert.FromBase64String(item.FileDataAsBase64String);
                var downloadDirectory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
                var filePath = Path.Combine(downloadDirectory, "test.pdf");

                var streamWriter = File.Create(filePath);
                streamWriter.Close();
                File.WriteAllBytes(filePath, base64EncodedBytes);

Я могу найти загруженный файл в папке «Загрузки», но также хочу показать уведомление в уведомлении. запретить загрузку файла и щелчок в уведомлениях, чтобы пользователь мог открыть загруженный файл.

Возможно ли это?

1 Ответ

1 голос
/ 16 октября 2019

Вы можете использовать Plugin.LocalNotification , чтобы показать уведомление после загрузки файла.

try
{
    var base64EncodedBytes = System.Convert.FromBase64String(item.FileDataAsBase64String);
    var downloadDirectory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
    var filePath = Path.Combine(downloadDirectory, "test.pdf");

    var streamWriter = File.Create(filePath);
    streamWriter.Close();
    File.WriteAllBytes(filePath, base64EncodedBytes);

    DisplayNotification("test.pdf downloaded successfully", filePath);
}
catch(System.Exception e)  
{  
    System.Console.WriteLine(e.ToString());  
    DisplayNotification("Download Failed",string.Empty);
} 


public void DisplayNotification(string message, string filePath)
{
    var notification = new NotificationRequest
    {
        NotificationId = 100,
        Title = "Your App name",
        Description = message,
        ReturningData = filePath, // Returning data when tapped on notification.
        NotifyTime = DateTime.Now.AddSeconds(30) // Used for Scheduling local notification, if not specified notification will show immediately.
    };
    NotificationCenter.Current.Show(notification);
}

Примечание: Обязательно инициализировать настройку плагина воба проекта iOS и Android.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...