Reputation: 626
In my Xamarin forms, I download a file in the Android App using Web client. It saved in the local folder. This will mention using the android notification as below code
webClient.DownloadFile(filePath, localPath);
Notification.Builder builder = new Notification.Builder(Android.App.Application.Context)
.SetContentTitle("Download Successfully")
.SetContentText("Click here to open")
.SetSmallIcon(Resource.Drawable.abc_btn_colored_material);
Notification notification = builder.Build();
NotificationManager notificationManager =
Android.App.Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
I want to open the downloaded file from the notification. How could i do that. I am new to the Xamarin. Please Suggest a solution.
Upvotes: 0
Views: 3928
Reputation: 998
I use this
var docSaved = await DisplayActionSheet("Saved", "Open", "Ok", "File was successfull);
switch (docSaved)
{
case "Open":
{
Device.OpenUri(new Uri(Your_FilePath));
break;
}
case "Ok":
{
break;
}
}
Upvotes: 0
Reputation: 24460
For your notification you can create a PendingIntent
with an Intent
, which has Action View on the file. This would look something like:
var mimeTypeMap = MimeTypeMap.Singleton;
var mimeType = mimeTypeMap.GetMimeTypeFromExtension(System.IO.Path.GetExtension(filePath));
var intent = new Intent();
intent.SetAction(Intent.ActionView);
intent.SetDataAndType(Android.Net.Uri.Parse(filePath), mimeType);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
Then you set that Intent
in your Notification
builder:
notification.SetContentIntent(pendingIntent);
Now, the filePath you give the Intent
must be somewhere that is accessible from all applications on Android, otherwise opening it will fail. So when you save the file, save it to External Storage in a public place.
Upvotes: 1