Android 11及以上MediaStyle通知中小图标不显示问题求助
解决Android 11+ MediaStyle通知小图标不显示问题
可能的原因及对应解决方案
1. 调整Badge图标类型设置
你当前设置了setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE),在Android 11及以上的MediaStyle通知中,该设置可能导致系统优先使用大图标作为Badge,同时隐藏状态栏的小图标。尝试修改为使用小图标作为Badge:
builder.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
若需保留大图标作为Badge且显示状态栏小图标,可显式开启通知Badge显示:
builder.setShowBadge(true)
2. 确保MediaSession处于活跃状态
MediaStyle通知依赖MediaSession的状态,若未正确激活,系统可能异常处理通知图标。显示通知前务必激活MediaSession:
mMediaSessionCompat.setActive(true);
3. 调整SmallIcon的设置时机
尝试将setSmallIcon调用放在MediaStyle配置之后,避免样式覆盖小图标设置:
builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle() .setShowActionsInCompactView(0,1,2) .setMediaSession(mMediaSessionCompat.getSessionToken())); // 移至此处设置小图标 builder.setSmallIcon(R.drawable.notification_transparent);
4. 检查SmallIcon资源合规性
Android 11+对通知图标要求更严格,确保notification_transparent是符合规范的单色图标(建议白色前景+透明背景)。可先替换为系统默认图标测试:
builder.setSmallIcon(android.R.drawable.stat_notify_chat)
完整修改示例代码
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID); // 先激活MediaSession mMediaSessionCompat.setActive(true); builder.setContentTitle("title") .setContentText("context") .setSubText("text") .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL) .setShowBadge(true) .setLargeIcon(iconLarge) .setContentIntent(pIntent) .setColor(ContextCompat.getColor(getInstance().getmContext(), R.color.lightColor)) .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,PlaybackStateCompat.ACTION_STOP)); builder.addAction(...1...); builder.addAction(...2...); builder.addAction(...3...); builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle() .setShowActionsInCompactView(0,1,2) .setMediaSession(mMediaSessionCompat.getSessionToken())); builder.setSmallIcon(R.drawable.notification_transparent); mNotificationManager.notify(NOTIFICATION_ID,builder.build());
内容的提问来源于stack exchange,提问作者GGK stands for Ukraine
相关产品推荐
相关产品推荐

