fix(android): allow notification tap to foreground app (#3831)

This commit is contained in:
Paul Rinaldi 2024-05-28 02:23:56 -05:00 committed by GitHub
parent ce9a0cdfa2
commit 5c29b48747
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -139,6 +139,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -842,7 +843,8 @@ public class ReactExoplayerView extends FrameLayout implements
playbackServiceBinder = (PlaybackServiceBinder) service; playbackServiceBinder = (PlaybackServiceBinder) service;
try { try {
playbackServiceBinder.getService().registerPlayer(player); playbackServiceBinder.getService().registerPlayer(player,
Objects.requireNonNull((Class<Activity>) (themedReactContext.getCurrentActivity()).getClass()));
} catch (Exception e) { } catch (Exception e) {
DebugLog.e(TAG, "Cloud not register ExoPlayer"); DebugLog.e(TAG, "Cloud not register ExoPlayer");
} }

View File

@ -1,8 +1,10 @@
package com.brentvatne.exoplayer package com.brentvatne.exoplayer
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity
import android.app.NotificationChannel import android.app.NotificationChannel
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Binder import android.os.Binder
@ -23,6 +25,7 @@ class PlaybackServiceBinder(val service: VideoPlaybackService) : Binder()
class VideoPlaybackService : MediaSessionService() { class VideoPlaybackService : MediaSessionService() {
private var mediaSessionsList = mutableMapOf<ExoPlayer, MediaSession>() private var mediaSessionsList = mutableMapOf<ExoPlayer, MediaSession>()
private var binder = PlaybackServiceBinder(this) private var binder = PlaybackServiceBinder(this)
private var sourceActivity: Class<Activity>? = null
// Controls // Controls
private val commandSeekForward = SessionCommand(COMMAND_SEEK_FORWARD, Bundle.EMPTY) private val commandSeekForward = SessionCommand(COMMAND_SEEK_FORWARD, Bundle.EMPTY)
@ -44,10 +47,11 @@ class VideoPlaybackService : MediaSessionService() {
// Player Registry // Player Registry
fun registerPlayer(player: ExoPlayer) { fun registerPlayer(player: ExoPlayer, from: Class<Activity>) {
if (mediaSessionsList.containsKey(player)) { if (mediaSessionsList.containsKey(player)) {
return return
} }
sourceActivity = from
val mediaSession = MediaSession.Builder(this, player) val mediaSession = MediaSession.Builder(this, player)
.setId("RNVideoPlaybackService_" + player.hashCode()) .setId("RNVideoPlaybackService_" + player.hashCode())
@ -63,6 +67,7 @@ class VideoPlaybackService : MediaSessionService() {
hidePlayerNotification(player) hidePlayerNotification(player)
val session = mediaSessionsList.remove(player) val session = mediaSessionsList.remove(player)
session?.release() session?.release()
sourceActivity = null
if (mediaSessionsList.isEmpty()) { if (mediaSessionsList.isEmpty()) {
cleanup() cleanup()
@ -110,9 +115,13 @@ class VideoPlaybackService : MediaSessionService() {
return return
} }
val returnToPlayer = Intent(this, sourceActivity).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val notificationCompact = NotificationCompat.Builder(this, NOTIFICATION_CHANEL_ID) val notificationCompact = NotificationCompat.Builder(this, NOTIFICATION_CHANEL_ID)
.setSmallIcon(androidx.media3.session.R.drawable.media3_icon_circular_play) .setSmallIcon(androidx.media3.session.R.drawable.media3_icon_circular_play)
.setStyle(MediaStyleNotificationHelper.MediaStyle(session)) .setStyle(MediaStyleNotificationHelper.MediaStyle(session))
.setContentIntent(PendingIntent.getActivity(this, 0, returnToPlayer, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
.build() .build()
notificationManager.notify(session.player.hashCode(), notificationCompact) notificationManager.notify(session.player.hashCode(), notificationCompact)