fix(android): notification and app name retrieval (#4781)

This commit is contained in:
Krzysztof Moch
2025-11-23 14:00:20 +01:00
committed by GitHub
parent 72f0a56676
commit 99d3861c8c
4 changed files with 45 additions and 18 deletions
+3 -3
View File
@@ -51,7 +51,7 @@
},
"example": {
"name": "react-native-video-example",
"version": "7.0.0-alpha.8",
"version": "7.0.0-alpha.9",
"dependencies": {
"@react-native-community/slider": "^4.5.6",
"@react-native-video/drm": "*",
@@ -79,7 +79,7 @@
},
"packages/drm-plugin": {
"name": "@react-native-video/drm",
"version": "7.0.0-alpha.8",
"version": "7.0.0-alpha.9",
"devDependencies": {
"@react-native/babel-preset": "0.79.2",
"@release-it/conventional-changelog": "^9.0.2",
@@ -107,7 +107,7 @@
},
"packages/react-native-video": {
"name": "react-native-video",
"version": "7.0.0-alpha.8",
"version": "7.0.0-alpha.9",
"devDependencies": {
"@expo/config-plugins": "^10.0.2",
"@react-native/eslint-config": "^0.77.0",
@@ -84,9 +84,13 @@ class CustomMediaNotificationProvider(context: Context) : DefaultMediaNotificati
private fun getAppName(): String {
return try {
val context = getContext()
val pm = context.packageManager
val label = pm.getApplicationLabel(context.applicationInfo)
label.toString()
val applicationInfo = context.applicationInfo
val labelRes = applicationInfo.labelRes
if (labelRes != 0) {
context.getString(labelRes)
} else {
applicationInfo.nonLocalizedLabel?.toString() ?: context.packageManager.getApplicationLabel(applicationInfo).toString()
}
} catch (e: Exception) {
return "Unknown"
}
@@ -31,6 +31,7 @@ class VideoPlaybackService : MediaSessionService() {
private var binder = VideoPlaybackServiceBinder(this)
private var sourceActivity: Class<Activity>? = null // retained for future deep-links; currently unused
private var isForeground = false
private var cachedLaunchIntent: Intent? = null
override fun onCreate() {
super.onCreate()
@@ -65,13 +66,20 @@ class VideoPlaybackService : MediaSessionService() {
// Ensure tapping the notification opens the app via sessionActivity
try {
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
var launchIntent = cachedLaunchIntent
if (launchIntent == null) {
launchIntent = packageManager.getLaunchIntentForPackage(packageName)
cachedLaunchIntent = launchIntent
}
if (launchIntent != null) {
launchIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
// Clone the intent before modifying it to avoid mutating the cached instance
val intentToUse = launchIntent.clone() as Intent
intentToUse.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
val contentIntent = PendingIntent.getActivity(
this,
0,
launchIntent,
intentToUse,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
builder.setSessionActivity(contentIntent)
@@ -175,19 +183,30 @@ class VideoPlaybackService : MediaSessionService() {
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Media playback",
NotificationManager.IMPORTANCE_LOW
)
channel.setShowBadge(false)
nm.createNotificationChannel(channel)
if (nm.getNotificationChannel(NOTIFICATION_CHANNEL_ID) == null) {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
"Media playback",
NotificationManager.IMPORTANCE_LOW
)
channel.setShowBadge(false)
nm.createNotificationChannel(channel)
}
} catch (_: Exception) {
Log.e(TAG, "Failed to create notification channel!")
}
}
val appName = try { applicationInfo.loadLabel(packageManager).toString() } catch (_: Exception) { "Media Playback" }
val appName = try {
val labelRes = applicationInfo.labelRes
if (labelRes != 0) {
getString(labelRes)
} else {
applicationInfo.nonLocalizedLabel?.toString() ?: applicationInfo.loadLabel(packageManager).toString()
}
} catch (_: Exception) {
"Media Playback"
}
return NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_media_play)
@@ -38,7 +38,11 @@ export const useVideoPlayer = (
factory: () => {
const player = new VideoPlayer(source);
if (player.source.config.initializeOnCreation) {
if (setup === undefined) {
return player;
}
if (player.source.config.initializeOnCreation !== false) {
// if source is small video, it can happen that onLoadStart is called before we set event from JS
// Thats why we adding event listener and calling setup once if player is loading or ready to play
// That way we ensure that setup is always called