简体   繁体   English

通知图标不会随 .setSmallIcon 改变

[英]Notification icon is not changed with .setSmallIcon

I am implementing Notifications in android .我正在android实现通知。 I want to change the icon of notification, but the problem is that setSmallIcon is not working.我想更改通知图标,但问题是setSmallIcon不起作用。 My custom icon is showing in the lollipop device but not showing the custom icon above lollipop devices.我的自定义图标显示在lollipop设备中,但未显示lollipop设备上方的自定义图标。

I have tried the following code:我尝试了以下代码:

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());
            }
}

The big picture is also not showing.大图也没有显示。 Please Help.请帮忙。

EDIT:编辑:

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());

Try uninstalling and reinstalling the 100% fresh App.尝试卸载并重新安装 100% 全新的应用程序。 Android keeps cache so things work more quickly and the notification is one of those things. Android 保留缓存,以便更快地工作,而通知就是其中之一。

Uninstalling and reinstalling worked for my issue some time ago.卸载和重新安装前一段时间对我的问题有效。

Also do remember that in Android Oreo and above, in order for notifications to work;还要记住,在 Android Oreo 及更高版本中,为了通知工作; notification channels must be created and also the notification that you're creating should be assigned to one of them.必须创建通知渠道,并且您正在创建的通知也应分配给其中之一。

EDIT编辑

What I've tried (and it works for me): (Sample in Kotlin but should be similar enough);我尝试过的(它对我有用):(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)
}

SECOND EDIT第二次编辑

I think there's a problem with your image, (as there was in mine; sizes or something).我认为你的形象有问题,(就像我的一样;尺寸或其他东西)。 After spending a lot of time trying to figure out why it didn't display the launcher icon in the notifications I decided to try with one of my images that I had laying around...在花了很多时间试图弄清楚为什么它没有在通知中显示启动器图标之后,我决定尝试使用我放置的其中一张图像......

Tested with Android O;用 Android O 测试; API 28原料药 28

And here are the results:结果如下:

折叠 展开

Feel free to try with the original image I used:随意尝试使用我使用的原始图像: WTF_Gopher

Here's the code that is powering these results;这是为这些结果提供动力的代码; it's not very different from the first one...和第一个没太大区别。。。

// 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)

Additional info:附加信息:

When I'm using a wrong image, I get this pesky log in my Logcat.当我使用错误的图像时,我的 Logcat 中会出现这个讨厌的日志。 Check for the same or similars!检查相同或相似的! 2019-04-10 20:11:02.120 3434-3434/com.example.deletemeapp D/skia: --- Failed to create image decoder with message 'unimplemented'

What you want to do is create the notification icon as follows:您要做的是创建通知图标如下:

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

Now Add:现在添加:

        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);

For the versioning add this:对于版本控制添加:

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

Hope this helps :)希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM