google / ExoPlayer

This project is deprecated and stale. The latest ExoPlayer code is available in https://github.com/androidx/media
https://developer.android.com/media/media3/exoplayer
Apache License 2.0
21.75k stars 6.03k forks source link

Notification gets automatically hidden sometime in Android 11 #9481

Open kmrinmoy07 opened 3 years ago

kmrinmoy07 commented 3 years ago

I am trying to show the music player's notification using PlayerNotificationManager but the notification is automatically getting hidden under some condition (may be when there is lots of other notifications) even when audio is being played. But the notification gets shown again when the state of the player gets changed, for example if I pause the audio the notification reappears on its own. Also I believe that it is only in Android 11.

I have even looked at all the articles available in the internet from last three days and tried to fix it but nothing is working so I thought to take help here, so hope someone gets me back about it. I have even asked a question in stackoverflow.

The piece of code that I am using for the notification is

private void createNotification() {
    PlayerNotificationManager.NotificationListener listener = new PlayerNotificationManager.NotificationListener() {
        @Override
        public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
            stopSelf();
        }

        @Override
        public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
            startForeground(notificationId, notification);
        }
    };
    playerNotificationManager = new PlayerNotificationManager.Builder(this, NOTIFICATION_ID, NOTIFICATION_CHANNEL)
        .setMediaDescriptionAdapter(new DescriptionAdapter())
        .setNotificationListener(listener)
        .setCustomActionReceiver(new CustomActionReceiver())
        .setChannelNameResourceId​(R.string.channel)
        .setChannelImportance​(NotificationUtil.IMPORTANCE_HIGH)
        .build();
    mediaSession = new MediaSessionCompat(getApplicationContext(), getResources().getString(R.string.app_name));
    mediaSession.setActive(true);
    playerNotificationManager.setMediaSessionToken(mediaSession.getSessionToken());
    mediaSessionConnector = new MediaSessionConnector(mediaSession);
    mediaSessionConnector.setPlayer(exoPlayer);
    playerNotificationManager.setPlayer(exoPlayer);
}
kmrinmoy07 commented 3 years ago

Hi, Can anyone of you please look into this issue

marcbaechinger commented 3 years ago

In Android 11 the notification is turned into a mini player on top of the notification. If there is another media app with a media session there are multiple such mini players.

I think this is a duplicate of #8500 and/or #9275. Can you please check there?

I close this issue. Please re-open if you think your issue is different.

kmrinmoy07 commented 3 years ago

Hi @marcbaechinger , this is not the case that you have said. I am already aware of the changes in Android 11 for music playback notification and it is not the duplicate of the mentioned issues.

In my case the notification is visible initially but later on, only in some cases under some certain condition the notification automatically gets hidden even if the user has not done anything with it and audio is being played. Also the notification reappears on its own (sometimes) when the state of the player gets changed, for example if I paused the audio the notification reappears on its own. I have seen this on Mi 9T Pro or Redmi K20 Pro as well in Vivo devices with Android 11.

Would request you to kindly reopen the issue.

marcbaechinger commented 3 years ago

Each time the state of the player has changed, a new notification is posted to reflect the new state. That is probably what you observe when the notification reappears.

Difficult to say with the available information. It is possibly related to the life cycle of the service in which you have the player and the notification manager. This is a guess and probably a long shot but that is what I would look into for investigation.

I would change the implementation of the notification listener to something like below:

@Override
public void onNotificationPosted(
    int notificationId, Notification notification, boolean ongoing) {
  if (ongoing) {
    startForeground(notificationId, notification);
  } else {
    stopForeground(/* removeNotification= */ false);
  }
}

Then add some log statements in your service's life cycle methods (onCreate(), onStartCommand() and onDestroy()) as well as in the notification listener's callback methods, so you are aware when the service is created, foregrounded, backgrounded and destroyed. It has possibly something to do with that. Perhaps you find some strange or surprising behaviour of the system or the app that is starting/stopping your service. I would also review when in the life-cycle of the service your createNotification method is called and how often (it should be called only once in the life-time of your service when I look at the code). Make sure that never a second instance of the notification manager is created. It's about hings like that which I would look at.

kmrinmoy07 commented 3 years ago

Hi @marcbaechinger earlier I also thought may be it is an issue with my service lifecycle but the audio still continues playing even after the notification disappears and since notification and audio player are of the same service so I believe may be it is not due to service lifecycle. But still as you suggested me, I believe that I must try putting log and on service's lifecycle & in notification listener's callback methods.

Also as you told that when state of the player has changed, a new notification is posted to reflect the new state, it is not the case in mine as I only create notification in the very first time when the service is started. Also I have only a single instance of the notification manager. Also I will try to share you more of my code when I am back to home after few days.

kmrinmoy07 commented 3 years ago

Hi, @marcbaechinger I did tried to investigate the issue and was not able to find any issue until yesterday when I noticed that the notification disappeared and I noticed that at that time I had lots of other notification by other apps. Can it be that Android system dismissed it when there was plenty of notifications ? Also as I said earlier that since my audio was still playing so I find it hard to guess if it is due to the service lifecycle. Let me also know if you need my whole service code sample beside the code that I have already posted, i will be happy to share you the complete service class by email if it is required.