I make an android music apps in kotlin that use media session notification, everything works well in another phone, but in Samsung Android 13 it not show in lockscreen.
In emulator it works,in xiami works also, and in samsung android 10 works also
here the code :
override fun onCreate() {
super.onCreate()
mm = MusicManager.getInstance(this)
mm?.setMediaPlayerListener(this)
mediaSession = MediaSessionCompat(this, "PlayerService")
mediaSession.isActive = true
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
LocalBroadcastManager.getInstance(this).registerReceiver(
receiver,
IntentFilter("notification-action")
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"channel_id",
"Channel Name",
NotificationManager.IMPORTANCE_DEFAULT
)
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager?.createNotificationChannel(channel)
}
}
private fun initializeNotification() {
imageLoader.loadImageFromUrl(MusicManager.MusicDataProvider.getMusicData()?.image?.l ?: "")
}
private fun updateNotification() {
updateMetadata()
val playPauseIcon =
if (isPlaying) R.drawable.ic_resume_music_white else R.drawable.ic_play_music_white
val notificationFlags = if (isPlaying) {
NotificationCompat.FLAG_ONGOING_EVENT
} else {
0 // Clear any ongoing event flag when paused
}
val musicImageBitmap = ContextCompat.getDrawable(this, R.drawable.img_lifestyle_music) as BitmapDrawable
// Build the notification
val builder =
NotificationCompat.Builder(this, "channel_id")
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.logo)
.setColorized(false)
.setContentIntent(
PendingIntent.getActivity(
this, 0, pendingIntent,
PendingIntent.FLAG_IMMUTABLE
)
)
.setContentTitle(MusicManager.MusicDataProvider.getMusicData()?.songName)
.setContentText(MusicManager.MusicDataProvider.getMusicData()?.artistName)
.setLargeIcon(image ?: musicImageBitmap.bitmap)
.setStyle(
androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSession.sessionToken)
.setShowActionsInCompactView(0, 1, 2)
)
.addAction(
R.drawable.ic_previous_music_white,
"Previous",
getPendingIntentForAction("previous")
)
.addAction(
playPauseIcon,
"Play",
getPendingIntentForAction("playPause")
)
.addAction(
R.drawable.ic_next_music_white,
"Next",
getPendingIntentForAction("next")
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setOnlyAlertOnce(true)
.setOngoing(isPlaying)
.build()
.apply {
flags = flags or notificationFlags
}
// Notify with the updated notification
if (
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS
)
!= PackageManager.PERMISSION_GRANTED
) {
return
}
startForeground(100, builder)
}
In emulator it works,in xiami works also, and in samsung android 10 works also