I show a local notification after a file download complete
String fileName = AppUtils.getFileNameFromUrlPath(download.getFile());
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelId)
.setSmallIcon(R.drawable.ic_fluxble_icon_primary)
.setAutoCancel(true)
.setContentTitle("Download Complete")
.setContentText(fileName);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
File file = new File(download.getFile());
Intent intent = AppUtils.getOpenApp(mContext, file);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(notificationId, mBuilder.build());
On notification click i open/show that file either image/doc etc to user.
public static Intent getOpenApp(Context context, File url) {
Intent intent = null;
try {
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", url);
intent = new Intent(Intent.ACTION_VIEW);
if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
// Word document
intent.setDataAndType(uri, "application/msword");
} else if (url.toString().contains(".pdf")) {
// PDF file
intent.setDataAndType(uri, "application/pdf");
} else if (url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
// Powerpoint file
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if (url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
// Excel file
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if (url.toString().contains(".zip")) {
// ZIP file
intent.setDataAndType(uri, "application/zip");
} else if (url.toString().contains(".rar")) {
// RAR file
intent.setDataAndType(uri, "application/x-rar-compressed");
} else if (url.toString().contains(".rtf")) {
// RTF file
intent.setDataAndType(uri, "application/rtf");
} else if (url.toString().contains(".wav") || url.toString().contains(".mp3")) {
// WAV audio file
intent.setDataAndType(uri, "audio/x-wav");
} else if (url.toString().contains(".gif")) {
// GIF file
intent.setDataAndType(uri, "image/gif");
} else if (url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) {
// JPG file
intent.setDataAndType(uri, "image/jpeg");
} else if (url.toString().contains(".txt")) {
// Text file
intent.setDataAndType(uri, "text/plain");
} else if (url.toString().contains(".3gp") || url.toString().contains(".mpg") ||
url.toString().contains(".mpeg") || url.toString().contains(".mpe") || url.toString().contains(".mp4") || url.toString().contains(".avi")) {
// Video files
intent.setDataAndType(uri, "video/*");
} else {
intent.setDataAndType(uri, "*/*");
}
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} catch (ActivityNotFoundException e) {
AppUtils.showSnackbar((Activity) context, "No application found which can open the file");
}
return intent;
}
Which is working fine. When i click on notification it shows image by Intent.ACTION_VIEW intent but when i click back button it does not get back to my app instead it gets to mobile desktop screen. My app gets in pause/minimize mode.
It is clear now what the problem is. You are building an Intent
to view a document using implicit intent (ACTION = VIEW) and putting that in a Notification
. When the user opens the Notification
and taps on it, the Intent
which you have constructed is launched. This doesn't launch your app, it launches whatever app the user needs to VIEW the file. Your app doesn't even need to be running.
When the users clicks BACK, it just goes to the HOME screen because your app didn't launch the viewer, Android did.
If you want the user to go BACK to your app, you need to do this in a different way. The Notification
should launch YOUR app, and then YOUR app should launch the VIEWer app. Then, when the user goes BACK, he will see YOUR app.