Reputation: 31
I'm new to android development, I'm building an application that supports playing video in the background. I decided to do it through Exo Player and PlayerNotificationManager. On Android 12 and below, everything works fine, but on Android 13, the notification does not want to be displayed at all, any ideas?
I use MVVM architecture, in my viewmodel's init method I initialize exoPlayer and playerNotificationManager
initialization code:
playerNotificationManager = PlayerNotificationManager.Builder(
getApplication<Application>().applicationContext,
1,
EXO_PLAYER_CHANNEL_ID)
.setSmallIconResourceId(R.drawable.racoon)
.setMediaDescriptionAdapter(mediaDescriptionAdapter)
.setFastForwardActionIconResourceId(R.drawable.ic_forward_10sec)
.setRewindActionIconResourceId(R.drawable.ic_replay_10sec)
.build()
After that, in my fragment, I assign exoPlayer to my playerNotificationManager in the onResume and onStop methods:
override fun onResume() {
super.onResume()
Log.d("MyLog", "onResume")
if (videoViewModel.playerNotificationManager != null){
videoViewModel.playerNotificationManager?.setPlayer(null)
}
}
override fun onStop() {
super.onStop()
Log.d("MyLog", "onStop")
videoViewModel.playerNotificationManager?.setPlayer(videoViewModel.exoPlayer)
}
Also I tried to register the following permissions in my manifest:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
But the result remained the same, android 13 does not want to display my notification at all
Upvotes: 1
Views: 1291
Reputation: 31
Based on @primo comment and on this links:
I did the following to solve my problem:
In onCreate method of my fragment:
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
val launcher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean? ->
notificationGranted = isGranted == true
}
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
In onStop method:
if(VERSION.SDK_INT >= VERSION_CODES.TIRAMISU && !notificationGranted){
videoViewModel.exoPlayer.pause()
}
videoViewModel.playerNotificationManager?.setPlayer(videoViewModel.exoPlayer)
And then everything works fine.
Upvotes: 2