您好,我正在构建一个流音乐应用程序,当我尝试设置 setNotificationListener 时,我收到此错误并且应用程序崩溃了
郑重声明,我已经可以显示通知,但重新安装应用程序后我收到此错误
这是我的代码
public void startToPlay(Context context){
// Global settings.
playerNotificationManagerBuilder = new PlayerNotificationManager.Builder(context,
PLAYBACK_NOTIFICATION_ID,
PLAYBACK_CHANNEL_ID);
playerNotificationManagerBuilder.setSmallIconResourceId(R.drawable.ic_image_ip);
playerNotificationManagerBuilder.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
@Override
public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
PlayerNotificationManager.NotificationListener.super.onNotificationCancelled(notificationId, dismissedByUser);
stopSelf();
}
@Override
public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
PlayerNotificationManager.NotificationListener.super.onNotificationPosted(notificationId, notification, ongoing);
if (ongoing) {
// Here Audio is playing, so we need to make sure the service will not get destroyed by calling startForeground.
startForeground(notificationId, notification);
} else {
//Here audio has stopped playing, so we can make notification dismissible on swipe.
stopForeground(false);
}
}
});
只需在调用之前创建一个通知通道
startForeground(notificationId, notification)
override fun onNotificationPosted(
notificationId: Int,
notification: Notification,
ongoing: Boolean
) {
// create channel for the audio player notificaiton
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(
"audio_player",
"channel_name",
importance
)
channel.setSound(null, null)
notificationManager.createNotificationChannel(channel)
}
startForeground(notificationId, notification)
}
就我而言,我是这样做的
在你的Manifest.xml中
<service android:name=".ForegroundService"
android:foregroundServiceType="mediaPlayback"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
<property android:name="android.app.FOREGROUND_SERVICE_MEDIA_PLAYBACK"
android:value="test"/>
</service>
<receiver android:name="androidx.media3.session.MediaButtonReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
在您的服务中 包 com.example
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import androidx.media3.common.util.UnstableApi
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
import androidx.media3.ui.PlayerNotificationManager
class ForegroundService: MediaSessionService() {
private lateinit var playerNotificationManager: PlayerNotificationManager
private var notificationManager: NotificationManager? = null
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession? {
return null
}
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
addNotificationToPlayer()
return START_NOT_STICKY
}
override fun onCreate() {
super.onCreate()
createNotificationChannel()
}
@UnstableApi
private fun addNotificationToPlayer() {
playerNotificationManager = PlayerNotificationManager.Builder(
this, 1, "NOTIFICATION_CHANNEL_ID"
).setNotificationListener(notificationListener()).build()
}
@UnstableApi
private fun notificationListener() = object : PlayerNotificationManager.NotificationListener {
override fun onNotificationPosted(
notificationId: Int,
notification: Notification,
ongoing: Boolean
) {
super.onNotificationPosted(notificationId, notification, ongoing)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
startForeground(
notificationId,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
)
} else {
startForeground(
notificationId,
notification
)
}
}
override fun onNotificationCancelled(notificationId: Int, dismissedByUser: Boolean) {
super.onNotificationCancelled(notificationId, dismissedByUser)
stopSelf()
}
}
private fun createNotificationChannel(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"NOTIFICATION_CHANNEL_ID",
"NOTIFICATION_CHANNEL_NAME",
NotificationManager.IMPORTANCE_LOW
)
getManager(this).createNotificationChannel(channel)
}
}
private fun getManager(context: Context): NotificationManager {
if (notificationManager == null) {
notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
return notificationManager as NotificationManager
}
}