更改通知 RemoteViews 背景颜色

Posted

技术标签:

【中文标题】更改通知 RemoteViews 背景颜色【英文标题】:Changing Notification RemoteViews Background Color 【发布时间】:2016-10-27 14:08:47 【问题描述】:

我在使用应用程序主题更改背景颜色时遇到问题。

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

我已经尝试过了,但我的通知背景仍然是白色的。 ID 是正确的,View linLayout 是一个 LinearLayout。

请记住:整个代码都是在服务中调用的!

谢谢!

【问题讨论】:

您似乎正在尝试构建媒体控制通知。请不要为此使用自定义通知,而是使用NotificationCompat.MediaStyle 好的,但为什么使用 MediaStyle 更好? 1) 适用于 API 7+ 设备 2) 使用 Lollipop+ 上的内置样式 3) 自动适应新的 android N 通知样式 4) 内置支持自定义背景颜色 5) 有内置支持解决在棒棒糖之前的设备上关闭来自前台服务的通知的问题 6) 支持添加您的 MediaSession 令牌以使 Android Wear 控件正常工作。仅举几例。 你应该看Best practices in media playback I/O 2016 talk 好的,谢谢 :) 【参考方案1】:

利用NotificationCompat.MediaStyle 可以更轻松地完成大部分工作。它从 API 24 之前的设备上的 setColor() 调用中提取背景颜色(并将该颜色用作 API 24+ 设备上的强调色)。这也意味着您不再需要编写任何自定义 RemoteViews 代码,因为它仅依赖于您添加到媒体控件通知中的操作:

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

您绝对应该阅读所有可用于MediaStyle 的方法,并查看Best Practices in media playback I/O 2016 talk 的示例代码和有关使用通知的最佳实践。具体在30 minutes into the talk

【讨论】:

你能发布一个示例代码吗?我试图找到一些有用的东西,但例如背景颜色保持黑色【参考方案2】:

setColorized 文档说:

设置此通知是否应着色。设置后,使用 setColor(int) 设置的颜色将用作此通知的背景颜色

这只能用于高优先级的持续任务,例如导航、正在进行的呼叫或其他类似的用户高优先级事件。

对于大多数样式,仅当通知用于前台服务通知时才会应用着色。

但是,对于附加了媒体会话的 MediaStyle 和 DecoratedMediaCustomViewStyle 通知,则没有这样的要求。

在 O 之前的任何版本上调用此方法都不会对通知产生影响,也不会被着色。

Kotlin 代码:

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

【讨论】:

以上是关于更改通知 RemoteViews 背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

当背景颜色为白色时,Android 通知图标颜色不会更改

Android 自定义Notification颜色适配问题

Android 自定义Notification颜色适配问题

如何更改android状态栏的背景颜色

如果使用 Parse Push,是不是可以为 Android 上的通知抽屉中的图标设置背景颜色?

通知生成器 setSmallIcon “背景”颜色