Android 通知图标在某些设备上是空白的

Sol*_*der 10 android push-notification android-assets android-notifications kotlin

我在某些 Android 设备(Pixel 2 XL 和 Pixel 5 - 均为 Android 11)上收到空白/灰色通知图标,但在我测试过的其他 Android 设备(华为 P20 和 P30 - 均为 Android 10)上显示正常。有没有其他人遇到过这种异常情况?

这是我尝试过的:

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())
    }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

use*_*081 1

我在 android 9、10、11 上测试过。效果很好。

在安卓工作室中:

  • 选择image asset
  • 点击icon type

这两种选择对我来说都非常有效。LMK

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())
    }
}
Run Code Online (Sandbox Code Playgroud)