【问题标题】:Android notification icon is blank on some devicesAndroid 通知图标在某些设备上为空白
【发布时间】:2021-02-21 08:39:22
【问题描述】:

我在某些 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())
    }

【问题讨论】:

  • R.mipmap.ic_stat_ic_notification 是图像还是矢量?您能否尝试仅使用 png 并删除所有矢量参考,看看是否能解决问题
  • 我也面临同样的问题。在我的情况下,我使用“cordova-plugin-fcm-with-dependecy-updated”插件,通知图标映射到“@mipmap/ic_launcher”。你找到解决办法了吗?

标签: android kotlin push-notification android-notifications android-assets


【解决方案1】:

我之前也遇到过类似的问题。就我而言,问题是我尝试使用的图标。我为通知的图标使用了非 PNG 文件。当我将其切换为 PNG 格式时,该通知对我测试的所有设备都正常工作。

如果你没有使用Png格式的图标,你可以试试这种方式。

另外,问题可能出在android reference website 中的图标大小,据说

设置小图标以在通知布局中使用。不同类别的设备可能返回不同的尺寸。

另请参阅 this guiadeline 了解关键线形状

【讨论】:

    【解决方案2】:

    我在 android 9、10、11 上进行了测试。它工作正常。 在android studio中,选择图片资产->点击图标类型->点击类似notification icon creation in 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())
        }
    }
    

    告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2019-10-29
      相关资源
      最近更新 更多