某些设备上的Android通知图标为空白。

10

在一些安卓设备上(Pixel 2 XL 和 Pixel 5 - 都是 Android 11),我遇到了一个空白/灰色的通知图标,但在我测试过的其他安卓设备(华为 P20 & P30 - 都是 Android 10)上,它显示得很好。还有其他人遇到这个异常吗?

这是我尝试过的:

  • 我确保所有图标大小都遵循使用 Android Asset Studio 定义的位图密度。

  • 我还向Notification.Builder对象添加了.setColorized()方法,因为之前的代码中缺少了它,但这没有任何改变。我在下面包含了我的通知代码:

private fun sendNotification(title: String?, message: String?, intent: Intent) {
    val pendingIntent = PendingIntent.getActivity(
        this,
        0 /* Request code */,
        intent,
        PendingIntent.FLAG_ONE_SHOT
    )

    // With the default notification channel id the heads up notification wasn't displayed
    val channelId = getString(R.string.heads_up_notification_channel_id)
    val notificationBuilder =
        NotificationCompat.Builder(this, channelId).setSmallIcon(R.mipmap.ic_stat_ic_notification)
            .setAutoCancel(true)
            .setColorized(true)
            .setColor(ContextCompat.getColor(this, R.color.color_orange))
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(Notification.PRIORITY_MAX)
            .setContentIntent(pendingIntent)


    if (title != null) {
        notificationBuilder.setContentTitle(title)

    }

    if (message != null) {
        notificationBuilder.setContentText(message).setStyle(
            NotificationCompat.BigTextStyle()
                .bigText(message)
        )
    }

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(
            channelId,
            "Test",
            NotificationManager.IMPORTANCE_HIGH
        )
        notificationManager.createNotificationChannel(channel)
    }

    notificationManager.notify(Random.nextInt()/* ID of notification */, notificationBuilder.build())
}

enter image description here


这里的 R.mipmap.ic_stat_ic_notification 是一张图片还是矢量图?你能否尝试只使用 png 格式并删除所有矢量图引用,看看是否可以解决问题? - AndroidEngineX
我也遇到了相同的问题.. 在我的情况下,我使用了“cordova-plugin-fcm-with-dependecy-updated”插件,通知图标映射为'@mipmap/ic_launcher.' 你找到解决办法了吗? - Sarath
2个回答

0

我在Android 9, 10和11上进行了测试,它可以正常工作。

在Android Studio中:

  • 选择图像资产
  • 点击图标类型

对我来说,这两个选项都很好用。让我知道一下。

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scrolling)
              sendNotification("Hello","thinking of refering friend?",intent);
        }


    private fun sendNotification(title: String?, message: String?, intent: Intent) {
        val pendingIntent = PendingIntent.getActivity(
                this,
                0 /* Request code */,
                intent,
                PendingIntent.FLAG_ONE_SHOT
        )

        // With the default notification channel id the heads up notification wasn't displayed
        val channelId = getString(R.string.heads_up_notification_channel_id)
        val notificationBuilder =
                NotificationCompat.Builder(this, channelId).setSmallIcon(R.drawable.abcd)
                        .setAutoCancel(true)
                        .setColorized(true)
                        .setColor(ContextCompat.getColor(this, R.color.color_orange))
                        .setDefaults(Notification.DEFAULT_ALL)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setContentIntent(pendingIntent)


        if (title != null) {
            notificationBuilder.setContentTitle(title)

        }

        if (message != null) {
            notificationBuilder.setContentText(message).setStyle(
                    NotificationCompat.BigTextStyle()
                            .bigText(message)
            )
        }

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                    channelId,
                    "Test",
                    NotificationManager.IMPORTANCE_HIGH
            )
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(Random.nextInt()/* ID of notification */, notificationBuilder.build())
    }
}

0

我之前也遇到过类似的问题。在我的情况下,问题出在我尝试使用的图标上。我使用了一个非PNG文件作为通知的图标。当我将其转换为PNG格式时,我测试的所有设备上的通知都正常工作。

如果您没有使用PNG格式的图标,则可以尝试这种方法。

此外,问题可能是您的图标大小,在android参考网站中指出:

设置用于通知布局的小图标。不同类别的设备可能返回不同的大小。

还可以查看此指南以获取关键线形状。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接