In my android app, using a progress notification, its working in all android versions except android O. In android O, file is getting downloaded but progress bar is not updating neither it shows "Download Complete". Below is my code which is under AsyncTask --
private class BackTask extends AsyncTask<String,Integer,Void> {
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
NotificationChannel notificationChannel;
protected void onPreExecute() {
super.onPreExecute();
mNotifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Uri selectedUri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
mBuilder = new NotificationCompat.Builder(context, null);
mBuilder.setContentTitle("Downloading - "+lblTitle.getText())
.setContentText("Download in progress")
.setSmallIcon(R.drawable.doupnowlogo)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
mNotifyManager.createNotificationChannel(notificationChannel);
}
else {
mBuilder.setContentTitle("Downloading - "+lblTitle.getText())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(ContextCompat.getColor(context, R.color.red))
.setVibrate(new long[]{100, 250})
.setLights(Color.YELLOW, 500, 5000)
.setAutoCancel(true);
}
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotifyManager.notify(1, mBuilder.build());
Toast.makeText(getActivity().getApplicationContext(), "Downloading the file...", Toast.LENGTH_SHORT).show();
}
protected Void doInBackground(String...params){
URL url;
int count;
try {
url = new URL(params[0].replaceAll(" ", "%20"));
String pathl="";
try {
File f=new File(storeDir);
if(f.exists()){
HttpURLConnection con=(HttpURLConnection)url.openConnection();
InputStream is=con.getInputStream();
String pathr=url.getPath();
String filename=pathr.substring(pathr.lastIndexOf('/')+1);
pathl=storeDir+"/"+filename;
FileOutputStream fos=new FileOutputStream(pathl);
int lenghtOfFile = con.getContentLength();
byte data[] = new byte[1024];
long total = 0;
while ((count = is.read(data)) != -1) {
total += count;
// publishing the progress
publishProgress((int)((total*100)/lenghtOfFile));
// writing data to output file
fos.write(data, 0, count);
}
is.close();
fos.flush();
fos.close();
}
else{
Log.e("Error","Not found: "+storeDir);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
mBuilder.setProgress(100, progress[0], false);
// Displays the progress bar on notification
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
}
protected void onPostExecute(Void result){
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0,0,false);
mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
The full program is working great in other versions, but getting the problem only in android o.
The problem in line:
You should use channel_id in builder constructor (you have added it below like
notificationChannel
) Also I see you created channel bellow like:I think it's better to use
NotificationManager.IMPORTANCE_LOW
for progress notifications becauseNotificationManager.IMPORTANCE_HIGH
will displays with pops up effect.So I think the best way to use two channels here. First one for starting/completed/error notifications and second one only for progress. And the first channel will use
NotificationManager.IMPORTANCE_DEFAULT
orNotificationManager.IMPORTANCE_HIGH
priority but progress channel -NotificationManager.IMPORTANCE_LOW
. In this implementation we could see start notification with sound vibration etc. and progress notifications without sound in system tray.It may help you, I am showing notifications in my current ongoing project. It shows notifications from 4.1 to latest 8.0 Oreo. Tested on each platform.
Here are my class private members :
In the same class i am creating and showing notifications with following code
JUST DO NOT USE
startForeground(1,new Notification());
, As i am in service and want to continue onstartcommand to get started.You may see the developer warning TOAST message on android 8.0 oreo and if you or any other member can resolve this issue too then it would be great for both of us.