.setSmallIcon不会更改通知图标

时间:2019-04-09 17:06:42

标签: java android android-notifications

我正在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());

2 个答案:

答案 0 :(得分:1)

尝试卸载并重新安装100%新鲜的App。 Android会保持高速缓存,因此事情可以更快地处理,而通知就是其中之一。

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

还请记住,在Android Oreo及更高版本中,为了使通知起作用;通知频道必须创建,并且您要创建的通知也应分配给其中一个。

编辑

我尝试过的方法(对我有用):(科特林的样本,但应该足够相似);

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

结果如下:

Collapsed Expanded

请随意尝试我使用的原始图像: 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'

答案 1 :(得分:0)

您要执行的操作如下创建通知图标:

        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
                .setContentTitle("New Book Added")
                .setContentText("Android with java");

现在添加:

        NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle().bigPicture(bitmap_image);
        s.setSummaryText("Summary text appears on expanding the notification");
        //Set the style of the notification to this big picture style
        builder.setStyle(s);

对于版本控制,请添加以下内容:

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
       // Marshmallow+
   }else{
        //below Marshmallow
}

希望这会有所帮助:)