Skip to content

Commit 99d3861

Browse files
fix(android): notification and app name retrieval (#4781)
1 parent 72f0a56 commit 99d3861

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

bun.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-native-video/android/src/main/java/com/twg/video/core/services/playback/CustomMediaNotificationProvider.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ class CustomMediaNotificationProvider(context: Context) : DefaultMediaNotificati
8484
private fun getAppName(): String {
8585
return try {
8686
val context = getContext()
87-
val pm = context.packageManager
88-
val label = pm.getApplicationLabel(context.applicationInfo)
89-
label.toString()
87+
val applicationInfo = context.applicationInfo
88+
val labelRes = applicationInfo.labelRes
89+
if (labelRes != 0) {
90+
context.getString(labelRes)
91+
} else {
92+
applicationInfo.nonLocalizedLabel?.toString() ?: context.packageManager.getApplicationLabel(applicationInfo).toString()
93+
}
9094
} catch (e: Exception) {
9195
return "Unknown"
9296
}

packages/react-native-video/android/src/main/java/com/twg/video/core/services/playback/VideoPlaybackService.kt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class VideoPlaybackService : MediaSessionService() {
3131
private var binder = VideoPlaybackServiceBinder(this)
3232
private var sourceActivity: Class<Activity>? = null // retained for future deep-links; currently unused
3333
private var isForeground = false
34+
private var cachedLaunchIntent: Intent? = null
3435

3536
override fun onCreate() {
3637
super.onCreate()
@@ -65,13 +66,20 @@ class VideoPlaybackService : MediaSessionService() {
6566

6667
// Ensure tapping the notification opens the app via sessionActivity
6768
try {
68-
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
69+
var launchIntent = cachedLaunchIntent
70+
if (launchIntent == null) {
71+
launchIntent = packageManager.getLaunchIntentForPackage(packageName)
72+
cachedLaunchIntent = launchIntent
73+
}
74+
6975
if (launchIntent != null) {
70-
launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
76+
// Clone the intent before modifying it to avoid mutating the cached instance
77+
val intentToUse = launchIntent.clone() as Intent
78+
intentToUse.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
7179
val contentIntent = PendingIntent.getActivity(
7280
this,
7381
0,
74-
launchIntent,
82+
intentToUse,
7583
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
7684
)
7785
builder.setSessionActivity(contentIntent)
@@ -175,19 +183,30 @@ class VideoPlaybackService : MediaSessionService() {
175183
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
176184
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
177185
try {
178-
val channel = NotificationChannel(
179-
NOTIFICATION_CHANNEL_ID,
180-
"Media playback",
181-
NotificationManager.IMPORTANCE_LOW
182-
)
183-
channel.setShowBadge(false)
184-
nm.createNotificationChannel(channel)
186+
if (nm.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
187+
val channel = NotificationChannel(
188+
NOTIFICATION_CHANNEL_ID,
189+
"Media playback",
190+
NotificationManager.IMPORTANCE_LOW
191+
)
192+
channel.setShowBadge(false)
193+
nm.createNotificationChannel(channel)
194+
}
185195
} catch (_: Exception) {
186196
Log.e(TAG, "Failed to create notification channel!")
187197
}
188198
}
189199

190-
val appName = try { applicationInfo.loadLabel(packageManager).toString() } catch (_: Exception) { "Media Playback" }
200+
val appName = try {
201+
val labelRes = applicationInfo.labelRes
202+
if (labelRes != 0) {
203+
getString(labelRes)
204+
} else {
205+
applicationInfo.nonLocalizedLabel?.toString() ?: applicationInfo.loadLabel(packageManager).toString()
206+
}
207+
} catch (_: Exception) {
208+
"Media Playback"
209+
}
191210

192211
return NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
193212
.setSmallIcon(android.R.drawable.ic_media_play)

packages/react-native-video/src/core/hooks/useVideoPlayer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export const useVideoPlayer = (
3838
factory: () => {
3939
const player = new VideoPlayer(source);
4040

41-
if (player.source.config.initializeOnCreation) {
41+
if (setup === undefined) {
42+
return player;
43+
}
44+
45+
if (player.source.config.initializeOnCreation !== false) {
4246
// if source is small video, it can happen that onLoadStart is called before we set event from JS
4347
// Thats why we adding event listener and calling setup once if player is loading or ready to play
4448
// That way we ensure that setup is always called

0 commit comments

Comments
 (0)