Search code examples
javaandroidnotificationsaudio-playerandroid-music-player

Android Notification not displaying action buttons


Below is the method I am using to build a notification for a media player application. Currently, the song information is being displayed correctly but the action buttons for prev, play, next are not being displayed.

This is what is displayed with the notification after calling buildNotification();

enter image description here

private void buildNotification(Notification.Action action) {
    String ID = "sage.musicplayer.Service.MusicService";

    /*Bitmap albumArtBitmap = null;
    if(album_art != null) {
        try {
            albumArtBitmap = Glide.with(getApplicationContext()).asBitmap().load(album_art).into(100, 100).get();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }else if(albumID > -1) {
        albumArtBitmap = musicUtils.getAlbumArt(albumID);
    }else{
        albumArtBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.new_album_art);
    }*/

    Intent intent = new Intent(MusicService.this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    Intent intentPrev = new Intent(ACTION_PREV);
    Intent intentPlayPause;
    if(isPng())
        intentPlayPause = new Intent(ACTION_PAUSE);
    else
        intentPlayPause = new Intent(ACTION_PLAY);
    Intent intentNext = new Intent(ACTION_NEXT);
    PendingIntent pendingPrev = PendingIntent.getActivity(this, 0, intentPrev, PendingIntent.FLAG_IMMUTABLE);
    PendingIntent pendingPlayPause = PendingIntent.getActivity(this, 0, intentPlayPause, PendingIntent.FLAG_IMMUTABLE);
    PendingIntent pendingNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_IMMUTABLE);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext());
    notification.setContentTitle(songTitle)
            .setWhen(System.currentTimeMillis())
            .setChannelId(ID)
            .setSmallIcon(R.drawable.logo)
            .setContentText(songArtist)
            .setLargeIcon(musicUtils.getAlbumArt(albumID))
            .setDeleteIntent(pendingIntent)
            .setPriority(Notification.PRIORITY_MAX)
            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle().setMediaSession(MediaSessionCompat.Token.fromToken(mediaSession.getSessionToken())))
            .addAction(R.drawable.prev, "Prev", pendingPrev)
            .addAction(R.drawable.playbutton, "Play", pendingPlayPause)
            .addAction(R.drawable.next, "Next", pendingNext);

    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
    notificationManagerCompat.notify(NOTIFY_ID, notification.build());
}

Solution

  • If your drawables (R.drawable.prev, etc) are vectorial and not images, it won't work. I had the same issue today.