同时在Android中同时更新两个通知

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

我遇到需要同时更新两个通知的情况。我有一个播放音频的服务,并且在播放音频时会显示播放器的通知。同样在另一项活动中,我有一种下载方法,该方法可以下载一些音频,并且在下载过程中,我需要显示下载进度的通知。因此,在某些情况下,如果同时发生上述两种情况,那么我需要同时显示两个具有不同的通知ID和不同的通知通道的不同通知。

现在,我告诉您,下载过程中的进度每秒钟都会更改,因此下载通知每秒钟都会更新。在此期间,如果任何人更改了音频源,并且需要更新播放器通知,那么就会出现主要问题,播放器通知不会得到更新,我只在下载通知被更新时才看到这种情况。

我正在使用AsyncTask下载并使用显示通知

  @Override
    protected void onPreExecute() {
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuilder = new NotificationCompat.Builder(context.get(), NOTIFICATION_CHANNEL_DOWNLOADING_ID)
                .setProgress(100, 0, true)
                .setContentTitle(context.get().getString(R.string.downloading) + "-" + name)
                .setContentText("0%")
                .setSmallIcon(R.mipmap.notification)
                .setChannelId(NOTIFICATION_CHANNEL_DOWNLOADING_ID)
                .setContentIntent(pendingIntent)
                .addAction(0, context.get().getResources().getString(R.string.cancel), cancelIntent)
                .setAutoCancel(true);


        NotificationChannel mChannel;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Download Notification";
            int importance = NotificationManager.IMPORTANCE_LOW;
            mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_DOWNLOADING_ID, name, importance);
            notificationManager.createNotificationChannel(mChannel);
        }

        notificationManager.notify(102, notificationBuilder.build());


 }

  @Override
    protected String doInBackground(String... strings) {
           //all the downloading stuff
           publishProgress(progress_value);
           ....
        }

  @Override
    protected void onProgressUpdate(Integer... progress) {
        notificationBuilder.setContentText(progress[0] + "%");
        notificationBuilder.setProgress(100, progress[0], false);
        //notification getting update every seconds due to continuous change in progress
        notificationManager.notify(102, notificationBuilder.build());


    }

现在在我的播放器服务中-

     notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon2))
                .setContentTitle(getString(R.string.app_name))
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.notification)
                .setTicker(Constant.arrayList_play.get(Constant.playPos).getMp3Name())
                .setChannelId(NOTIFICATION_CHANNEL_ID)
                .setOnlyAlertOnce(true);

      NotificationChannel mChannel;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Music Playback";
            int importance = NotificationManager.IMPORTANCE_LOW;
            mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
            mNotificationManager.createNotificationChannel(mChannel);
    }
      mNotificationManager.notify(101, notification.build());

我已经尝试通过增加间隔来更改进度更新持续时间,然后在以后的通知没有更新时播放器通知可以正常工作。

任何人都可以在这件事上帮助我,在此先感谢。

java android notifications
1个回答
0
投票

通知ID应该不同

mNotificationManager.notify(Notification_ID, notification.build());
© www.soinside.com 2019 - 2024. All rights reserved.