Notification small icon is not updating as expected

252 Views Asked by At

I am using foregroundService for playing audio in Background and show a notification with actions.

public NotificationHelper(ForegroundService service) {
    mFG_Service = service;

    mNotificationManager = (NotificationManager) mFG_Service.getSystemService(Context.NOTIFICATION_SERVICE);
}



public void showNotification(ChaptersBO chaptersBO, boolean isPlaying) {
    Notification notification = createNotification(chaptersBO, isPlaying);

    if (notification != null) {
        if (isPlaying) {
            mFG_Service.startForeground(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
        } else {
            mFG_Service.stopForeground(false);
            mNotificationManager.notify(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
        }
    }
}


private Notification createNotification(ChaptersBO chapter, boolean isPlaying) {
    if (chapter == null)
        return null;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mFG_Service);
    try {
        Bitmap sourceBitmap = BitmapFactory.decodeResource(mFG_Service.getResources(), R.drawable.notification_logo);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(sourceBitmap, 300, 300, false);
        if (resizedBitmap != sourceBitmap) {
            sourceBitmap.recycle();
        }

        builder.setContentTitle("Audio")
                .setContentText(chapter.getChapterNumber())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setSmallIcon(getSmallIcon(isPlaying))
                .setLargeIcon(resizedBitmap)
                .setShowWhen(false)
                .setOngoing(isPlaying)
                .setContentIntent(getPendingIntent())
                .addAction(R.drawable.ic_notif_prev, "", getAction(ACTION.PREV_ACTION, REQ_CODE_PREVIOUS))
                .addAction(getSmallIcon(isPlaying), "", getAction(ACTION.PLAY_PAUSE_ACTION, REQ_CODE_PLAY_OR_PAUSE))
                .addAction(R.drawable.ic_notif_next, "", getAction(ACTION.NEXT_ACTION, REQ_CODE_NEXT))
                .setDeleteIntent(getStopIntent())
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle());

    } catch (Exception e) {
        e.printStackTrace();
    }
    return builder.build();

}

I want to update notification small icon and center action icon on play/pause. For the first time it works fine but if I clear the notification and play another audio then these icons (Encircled in below screenshot) are not updating.

enter image description here

To pause audio I am calling this:

mNotificationManager.notify(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);

If I replace it with this

mFG_Service.startForeground(NOTIFICATION_ID.FOREGROUND_SERVICE, notification);

then it works fine but in that case I can't clear notification.

Any suggestions to update these icons on Play/Pause and also make the notification removable when audio is paused will be appreciated.

0

There are 0 best solutions below