你好,我正在构建一个流音乐应用程序,当我试图设置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);
}
}
});
2条答案
按热度按时间uurv41yg1#
在调用之前,只需创建通知通道
startForeground(notificationId, notification)
xzv2uavs2#
我是这样做的
在Manifest.xml中
在你的服务