0

I'm creating a simple notification with playback buttons for my audio player. But I have trouble setting the bitmap image for .setLargeIcon(). I'm trying to set it using glide. The thing is this image changes as user skips to next song, so this is pretty much a dynamic view. The image sometimes appears (for a split second), when I change the song or pause it. I'll be glad if you guys could help me!

Code:

Build notification:

    private void buildNotifications(PlaybackStatus playbackStatus){

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){

        createChannel();
    }



    int notificationAction = android.R.drawable.ic_media_pause;//needs to be initialized
    PendingIntent play_pauseAction = null;

    //Build a new notification according to the current state of the MediaPlayer
    if (playbackStatus == PlaybackStatus.PLAYING) {
        notificationAction = android.R.drawable.ic_media_pause;
        //create the pause action
        play_pauseAction = playbackAction(1);
    } else if (playbackStatus == PlaybackStatus.PAUSED) {
        notificationAction = android.R.drawable.ic_media_play;
        //create the play action
        play_pauseAction = playbackAction(0);
    }



   final NotificationCompat.Builder notiB = new NotificationCompat.Builder(this,CHANNEL_ID);
    notiB.setShowWhen(false);
    notiB.setStyle(new MediaStyle()
            .setMediaSession(mediaSession.getSessionToken())
            .setShowActionsInCompactView(0,1,2));
    notiB.setSmallIcon(R.drawable.play_song);
    Glide.with(this).asBitmap().load(songInfoModelService.getAlbumIDArtwork()).into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

            notiB.setLargeIcon(resource);
        }
    });
    notiB.setContentTitle(songInfoModelService.getSongName());
    notiB.setContentText(songInfoModelService.getArtistName());
    notiB.addAction(android.R.drawable.ic_media_previous, "previous", playbackAction(3));
    notiB.addAction(notificationAction, "pause", play_pauseAction);
    notiB.addAction(android.R.drawable.ic_media_next, "next", playbackAction(2));

    notification = notiB.build();
    notificationManager.notify(NOTIFICATION_ID,notification);


}
Rektirino
  • 582
  • 5
  • 24

1 Answers1

0

This will not work synchronously .
Glide.with(this).asBitmap().load() is an Asynchronous request so it will execute on separate thread regardless of your current thread . So in this case notiB.setLargeIcon(resource) never will get called synchronously.

Solution can be Show the notification inside onResourceReady(). Once you download the Bitmap then build the Notification .

ADM
  • 20,406
  • 11
  • 52
  • 83