【问题标题】:Updating two Android Notification simultaneously at the same time同时更新两个Android Notification
【发布时间】:2020-05-16 21:09:18
【问题描述】:

我遇到了需要同时更新两个通知的情况。我有一个播放音频的服务,在播放音频时我用来显示播放器的通知。同样在另一个活动中,我有一个下载方法,可以下载一些音频,在下载过程中我需要显示下载进度通知。所以在某些情况下,如果上述两种情况都发生了,那么我需要同时显示两个不同的通知,它们都具有不同的通知 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】:

    通知 ID 应该不同

    mNotificationManager.notify(Notification_ID, notification.build());
    

    【讨论】:

    • 感谢您的回答,但我已经使用了不同的通知 ID。我已经在问题中写过 - 都有不同的通知 ID 和不同的通知通道。
    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多