Skip to content

Commit

Permalink
fix(android): allow notification tap to foreground app (#3831)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrinaldi authored May 28, 2024
1 parent ce9a0cd commit 5c29b48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import java.util.Map;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -842,7 +843,8 @@ public void onServiceConnected(ComponentName name, IBinder service) {
playbackServiceBinder = (PlaybackServiceBinder) service;

try {
playbackServiceBinder.getService().registerPlayer(player);
playbackServiceBinder.getService().registerPlayer(player,
Objects.requireNonNull((Class<Activity>) (themedReactContext.getCurrentActivity()).getClass()));
} catch (Exception e) {
DebugLog.e(TAG, "Cloud not register ExoPlayer");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.brentvatne.exoplayer

import android.annotation.SuppressLint
import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Binder
Expand All @@ -23,6 +25,7 @@ class PlaybackServiceBinder(val service: VideoPlaybackService) : Binder()
class VideoPlaybackService : MediaSessionService() {
private var mediaSessionsList = mutableMapOf<ExoPlayer, MediaSession>()
private var binder = PlaybackServiceBinder(this)
private var sourceActivity: Class<Activity>? = null

// Controls
private val commandSeekForward = SessionCommand(COMMAND_SEEK_FORWARD, Bundle.EMPTY)
Expand All @@ -44,10 +47,11 @@ class VideoPlaybackService : MediaSessionService() {

// Player Registry

fun registerPlayer(player: ExoPlayer) {
fun registerPlayer(player: ExoPlayer, from: Class<Activity>) {
if (mediaSessionsList.containsKey(player)) {
return
}
sourceActivity = from

val mediaSession = MediaSession.Builder(this, player)
.setId("RNVideoPlaybackService_" + player.hashCode())
Expand All @@ -63,6 +67,7 @@ class VideoPlaybackService : MediaSessionService() {
hidePlayerNotification(player)
val session = mediaSessionsList.remove(player)
session?.release()
sourceActivity = null

if (mediaSessionsList.isEmpty()) {
cleanup()
Expand Down Expand Up @@ -110,9 +115,13 @@ class VideoPlaybackService : MediaSessionService() {
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)
.setSmallIcon(androidx.media3.session.R.drawable.media3_icon_circular_play)
.setStyle(MediaStyleNotificationHelper.MediaStyle(session))
.setContentIntent(PendingIntent.getActivity(this, 0, returnToPlayer, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
.build()

notificationManager.notify(session.player.hashCode(), notificationCompact)
Expand Down

0 comments on commit 5c29b48

Please sign in to comment.