Android O中通知中的进度条未更新

问题描述 投票:0回答:2

在我的Android应用程序中,使用进度通知,它在除了android O之外的所有Android版本中工作。在android O中,文件正在下载但进度条没有更新它既不显示“下载完成”。下面是我在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());
        }
    }

完整的程序在其他版本中运行良好,但只在android o中获得问题。

android push-notification progress-bar android-8.0-oreo
2个回答
0
投票

它可以帮到你,我正在我目前正在进行的项目中显示通知。它显示从4.1到最新8.0奥利奥的通知。在每个平台上测试。


这是我班级的私人会员:

private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "1";

在同一个类中,我使用以下代码创建和显示通知

try
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            startForeground(1,new Notification());

            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationManager.createNotificationChannel(notificationChannel);

            // notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_service_success)
                    .setContentTitle("Insta Promo")
                    .setContentText("We are ready to help you.");

            notificationManager.notify(NOTIFICATION_ID, builder.build());
        }
        else
        {
            final Intent intent = new Intent(this, WatchMan.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder b = new NotificationCompat.Builder(this,"mychannel");

            b.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_service_success)
                    .setTicker("Success")
                    .setContentTitle("Insta Promo")
                    .setContentText("We are ready to help you.")
                    .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND).setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
                    .setContentIntent(contentIntent)
                    .setContentInfo("Info");

            NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(1, b.build());

        }

    }
    catch(Exception e)
    {
        Log.d(TAG, "Null pointer exception xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...\n");
        System.out.println("Null Pointer ");
    }

只是不要使用startForeground(1,new Notification());,因为我在服务,并希望继续onstartcommand开始。


您可能会在android 8.0 oreo上看到开发人员警告TOAST消息,如果您或任何其他成员也可以解决此问题,那么对我们这两个人来说都会很棒。


0
投票

问题在于:

mBuilder = new NotificationCompat.Builder(context, null);

你应该在构建器构造函数中使用channel_id(你在notificationChannel下面添加了它)另外我看到你创建的频道如下:

notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

我认为最好使用NotificationManager.IMPORTANCE_LOW进行通知,因为NotificationManager.IMPORTANCE_HIGH将显示弹出效果。

所以我认为这里使用两个频道的最佳方式。第一个用于启动/完成/错误通知,第二个用于进度。第一个频道将使用NotificationManager.IMPORTANCE_DEFAULTNotificationManager.IMPORTANCE_HIGH优先级但进度频道-NotificationManager.IMPORTANCE_LOW。在此实现中,我们可以看到声音振动等的开始通知和系统托盘中没有声音的进度通知。

© www.soinside.com 2019 - 2024. All rights reserved.