javaandroidexoplayerbackground-service

Bad notification for startForeground playerNotificationManagerBuilder.setNotificationListener


Hello I'm building a stream music app and when I tried to set the setNotificationListener I receive this error and the app crashes

for the record, I already can show the notification but after reinstalling the app I receive this error

this is my code

     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);
                }
            }
        });

Solution

  • just create a channel for the notification before calling

    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)
        }