我是android开发的新手,我正在构建一个支持后台播放视频的应用程序。我决定通过Exo和PlayerNotificationManager来做。在Android 12和更低版本上,一切都很好,但是在Android 13上,通知根本不想显示,有什么想法吗?
我使用MVVM架构,在视图模型的init方法中初始化exoPlayer和playerNotificationManager
初始化代码:
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()之后,在我的片段中,我在exoPlayer和onStop方法中将playerNotificationManager分配给playerNotificationManager:
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)
}此外,我还试图在我的清单中注册以下权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>但是结果保持不变,android 13根本不想显示我的通知。
发布于 2022-11-18 16:11:17
基于@primo注释和此链接:
我做了以下工作来解决我的问题:
在我的片段的onCreate方法中:
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
val launcher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean? ->
notificationGranted = isGranted == true
}
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}在onStop方法中:
if(VERSION.SDK_INT >= VERSION_CODES.TIRAMISU && !notificationGranted){
videoViewModel.exoPlayer.pause()
}
videoViewModel.playerNotificationManager?.setPlayer(videoViewModel.exoPlayer)然后一切都很好。
https://stackoverflow.com/questions/74487687
复制相似问题