I've a problem changing my Background-Color with the Application-Theme.
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int iPrimaryColor = typedValue.data;
getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
int iPrimaryDarkColor = typedValue.data;
Intent notIntent = new Intent(getApplicationContext(), MainActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notOpenOnClick = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews smallContentView = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
nBuilder.setSmallIcon(R.drawable.not_icon)
.setOngoing(true)
.setContentTitle(getCurrentSong().getTitle())
.setContentIntent(notOpenOnClick);
Notification not = nBuilder.build();
smallContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor);
smallContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor);
bigContentView.setInt(R.id.not_linLayout, "setBackgroundColor", iPrimaryColor);
bigContentView.setInt(R.id.not_imvDivider, "setBackgroundColor", iPrimaryDarkColor);
setListeners(smallContentView);
setListeners(bigContentView);
not.contentView = smallContentView;
not.bigContentView = bigContentView;
if (isPlaying()) {
not.contentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_pause_48dp);
not.bigContentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_pause_48dp);
}
else {
not.contentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_play_48dp);
not.bigContentView.setImageViewResource(R.id.not_btnPlayPause, R.drawable.ic_play_48dp);
}
I already tried this, but the Background of my Notification is still white. The ID's are correct, the View linLayout is a LinearLayout.
Please keep in mind: The whole codes is invoked in a Service!
Thanks!
the setColorized documentation says:
Set whether this notification should be colorized. When set, the color set with setColor(int) will be used as the background color of this notification.
This should only be used for high priority ongoing tasks like navigation, an ongoing call, or other similarly high-priority events for the user.
For most styles, the coloring will only be applied if the notification is for a foreground service notification.
However, for MediaStyle and DecoratedMediaCustomViewStyle notifications that have a media session attached there is no such requirement.
Calling this method on any version prior to O will not have an effect on the notification and it won't be colorized.
Kotlin code:
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.core.app.NotificationCompat
import androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle
// create a MediaSession so that we can create a DecoratedMediaCustomViewStyle
val mediaSession = MediaSessionCompat(applicationContext,"tag")
mediaSession.setFlags(0)
mediaSession.setPlaybackState(PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_NONE,0,0f)
.build())
// create & display the colorized notification
notificationManager.notify(
0,
NotificationCompat
.Builder(this,notificationChannel)
.setStyle(DecoratedMediaCustomViewStyle()
.setMediaSession(mediaSession.sessionToken))
.setSmallIcon(R.drawable.ic_app_foreground)
.setColor(getColor(android.R.color.black))
.setColorized(true)
.setContentText("Hello, World!")
.build())
....
// cleanup upon dismissing the notification
mediaSession.release()
Much of this can be easier done by taking advantage of NotificationCompat.MediaStyle. It pulls the background color from the setColor() call on pre-API 24 devices (and uses that color as an accent on API 24+ devices). This also means you don't need to write any custom RemoteViews code anymore as it relies solely on the actions you add to your notification for media controls:
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
nBuilder.setSmallIcon(R.drawable.not_icon)
.setContentTitle(getCurrentSong().getTitle())
.setContentIntent(notOpenOnClick);
// This is what sets the background color on <N devices
// It is an accent color on N+ devices
nBuilder.setColor(getResources().getColor(R.color.colorPrimary));
// Add actions via nBuilder.addAction()
// Set the style, setShowActionsInCompactView(0) means the first
// action you've added will be shown the non-expanded view
nBuilder.setStyle(new NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0));
You should definitely read over all of the methods available for MediaStyle and reivew the Best Practices in media playback I/O 2016 talk for example code and best practices around using notifications. Specifically at 30 minutes into the talk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With