繁体   English   中英

Android - 通知图标不会改变

[英]Android - Notification icon will not change

如何更改通知中的图标和大图标? 我正在尝试这个代码:

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        Notification notification = builder
            .setSmallIcon(android.R.drawable.stat_notify_more)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentTitle("title")
            .setContentText("text").build();

        notificationManager.notify(666, notification);

这很好用,显示了通知。 但是通知仍然像application icon ,而不是来自 android drawable"stat_notifi_more ”,为什么?

而且STATUS BAR ICON (左上角电池、wifi 和其他旁边的通知小图标)也没有显示!

有人可以帮我解决这个问题吗?

编辑:我有棒棒糖 5.1.1 和 android 24 (sdk)

编辑2

通知图标 (OK) (96x96px):

在此处输入图片说明

状态栏图标 (24x24px)(不行,默认应用程序图标):

在此处输入图片说明

真实状态栏图标:

在此处输入图片说明

通知来源:

// Using RemoteViews to bind custom layouts into Notification
            RemoteViews remoteViews = new RemoteViews(getPackageName(),
                    R.layout.notification_layout);

            // Set Notification Title
            String strtitle = "title";
            // Set Notification Text
            String strtext = "text";

            // Open NotificationView Class on Notification Click
            Intent intent = new Intent(this, MainActivity.class);
            // Send data to NotificationView Class
            intent.putExtra("title", strtitle);
            intent.putExtra("text", strtext);
            // Open NotificationView.java Activity
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            // Locate and set the Image into customnotificationtext.xml ImageViews
            remoteViews.setImageViewResource(R.id.imagenotileft, R.drawable.stacionar_xhdpi_green);

            // Locate and set the Text into customnotificationtext.xml TextViews
            remoteViews.setTextViewText(R.id.title, "title");
            remoteViews.setTextViewText(R.id.text, "content");

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.wall_black)
                    .setTicker("t")
                    .setContentIntent(pIntent)
                    .setContent(remoteViews);

            // Create Notification Manager
            NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // Build Notification with Notification Manager
            notificationmanager.notify(0, builder.build());

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imagenotileft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imagenotileft" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_toRightOf="@+id/imagenotileft" />
</RelativeLayout>

Android 通知将始终显示应用程序图标。 您需要创建自定义通知布局来显示应用程序图标以外的图标。 在这里,您可以使用任何您想要的图标/设计。

setSmallIcon()设置出现在状态栏上而不是在通知托盘中的通知图标。 您在此处分配的可绘制对象的尺寸应该非常小。 此处检查预期尺寸。 我相信您的 drawable 比预期的尺寸大,因此无法渲染它。 因此,您无法在状态栏中看到它。

编辑:状态栏中的小图标将始终为白色,因此不会显示您的红色和绿色。 我认为您看不到绿色是因为颜色的半透明性。 即使您的 drawable 具有纯红色或绿色,它也只会显示为一个白色圆圈。 这个SO Question有关于它的详细信息。 因此,我的建议是,您只需为小图标使用默认应用程序图标,并使用您想要的任何自定义图标作为通知托盘中的通知。

如果您使用 Firebase Cloud Messaging: 要在应用程序处于后台时更改小图标,您可以从清单中执行此操作:

        <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/notification_icon"
        android:value="@string/default_notification_channel_id" />

暂无
暂无

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

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