首页 > 解决方案 > 通知图标未使用 .setSmallIcon 更改

问题描述

我正在实现通知android。我想更改通知图标,但问题是它setSmallIcon不起作用。我的自定义图标显示在lollipop设备中,但未在设备上方显示自定义图标lollipop

我尝试了以下代码:

send_notification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.book2);

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("New Book Added")
                        .setContentText("Android with java")
                        .setStyle(new NotificationCompat.BigPictureStyle()
                                    .bigPicture(icon)
                                    .bigLargeIcon(null));
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    builder.setSmallIcon(R.drawable.ic_notification);
                } else {
                    builder.setSmallIcon(R.drawable.notification);
                }
                NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                manager.notify(1,builder.build());
            }
}

大图也没有显示。请帮忙。

编辑:

Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.test);

    String channelId = getString(R.string.default_notification_channel_id);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);

    if (remoteMessage.getData().size() > 0) {
        String title = remoteMessage.getData().get("title");
        String text = remoteMessage.getData().get("body");

        builder.setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(text)
                .setLargeIcon(icon)
                .setPriority(Notification.PRIORITY_MAX)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigLargeIcon(icon));

    } else if (remoteMessage.getNotification() != null) {
        String title = remoteMessage.getNotification().getTitle();
        String text = remoteMessage.getNotification().getBody();

        builder.setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText(text)
                .setLargeIcon(icon)
                .setPriority(Notification.PRIORITY_MAX)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigLargeIcon(icon));

    }

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel notificationChannel = new NotificationChannel(channelId, "Testing channel", NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(notificationChannel);

    }
    manager.notify(1, builder.build());

标签: javaandroidandroid-notifications

解决方案


尝试卸载并重新安装 100% 全新的应用程序。Android 会保留缓存,以便更快地工作,而通知就是其中之一。

卸载和重新安装前段时间解决了我的问题。

还要记住,在 Android Oreo 及更高版本中,为了通知工作;必须创建通知渠道,并且您正在创建的通知应分配给其中之一。

编辑

我尝试过的(它对我有用):(Kotlin 中的示例,但应该足够相似);

fun onClick(view: View) {
    var icon_resource = R.drawable.ic_mtrl_chip_close_circle
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
        icon_resource = R.drawable.ic_mtrl_chip_checked_circle
    }

    val icon = BitmapFactory.decodeResource(resources,R.mipmap.ic_launcher_round)

    val notification =  NotificationCompat.Builder(this)
        .setSmallIcon(icon_resource)
        .setContentTitle("New Book Added")
        .setContentText("Android with java")
        .setLargeIcon(icon)
        .setStyle(NotificationCompat.BigPictureStyle()
            .bigPicture(icon)
            .bigLargeIcon(null)).build()

   val manager=  getSystemService(NOTIFICATION_SERVICE) as NotificationManager
   manager.notify(1,notification)
}

第二次编辑

我认为您的图像有问题,(就像我的一样;尺寸或其他东西)。在花了很多时间试图弄清楚为什么它没有在通知中显示启动器图标后,我决定尝试使用我放置的一张图像......

使用 Android O 测试;API 28

结果如下:

折叠 展开

随意尝试我使用的原始图像: WTF_Gopher

这是驱动这些结果的代码;和第一个差别不大。。。

// Parametherise this drawable with an if statement of your own
var icon_resource = R.drawable.ic_mtrl_chip_close_circle

val manager=  NotificationManagerCompat.from(this)
// This is required because the testing device is an Android O
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    manager.createNotificationChannel(
        NotificationChannel("CUSTOM_NOTIFICATIONS", "StackOverflow", NotificationManager.IMPORTANCE_HIGH)
    )
}

val icon = BitmapFactory.decodeResource(resources,R.mipmap.dafuq)

val notification =  NotificationCompat.Builder(this, "CUSTOM_NOTIFICATIONS")
    .setSmallIcon(icon_resource)
    .setContentTitle("New Book Added")
    .setContentText("Android with java")
    .setLargeIcon(icon)
    .setStyle(NotificationCompat.BigPictureStyle()
        .bigPicture(icon)
        .bigLargeIcon(null)).build()

manager.notify(1,notification)

附加信息:

当我使用错误的图像时,我会在我的 Logcat 中得到这个讨厌的日志。检查是否相同或相似! 2019-04-10 20:11:02.120 3434-3434/com.example.deletemeapp D/skia: --- Failed to create image decoder with message 'unimplemented'


推荐阅读