startForeground playerNotificationManagerBuilder.setNotificationListener 的错误通知
Bad notification for startForeground playerNotificationManagerBuilder.setNotificationListener
您好,我正在构建一个流媒体音乐应用程序,当我尝试设置 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)
}
您好,我正在构建一个流媒体音乐应用程序,当我尝试设置 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)
}