【问题标题】:How to Make a colored Notification Icon for my App如何为我的应用制作彩色通知图标
【发布时间】:2020-08-24 17:39:55
【问题描述】:

我有一个 PNG 图像,我想让它成为我收到任何通知时出现的通知图标,我不希望它是灰色和白色的,并且知道如何实现它?

FB 和 youtube 通知图标是彩色的

我的代码

 notificationBuilder.SetContentTitle("My-App")
                    .SetSmallIcon(Resource.Drawable.My_PNG_ICON)
                    .SetContentText(messageBody)
                    .SetAutoCancel(true)
                    .SetShowWhen(false)
                    .SetContentIntent(pendingIntent);

我在我的 PNG 图像中使用了这个链接,但它给出了一个白色和灰色的图标 Link

【问题讨论】:

标签: xamarin.forms


【解决方案1】:

notificationBuilder 调用链上,应该有一个名为SetColor 的方法。在尝试设置通知标题/图标颜色时使用它。

notificationBuilder.SetContentTitle("My-App")
                   .SetSmallIcon(Resource.Drawable.My_PNG_ICON)
                   .SetContentText(messageBody)
                   .SetAutoCancel(true)
                   .SetShowWhen(false)
                   .SetContentIntent(pendingIntent)
                   .SetColor(new Color().ToArgb()); // NEW LINE TO SET COLOR

您可能也需要这条线,但请测试以确保

.SetColorized(true)

【讨论】:

    【解决方案2】:

    notificationBuilder.SetSmallIcon 无法显示真实颜色,您可以自定义一个RemoteViews 用于notificationBuilder。然后可以显示你想要什么。

    示例代码如下:

    RemoteViews contentViews = new RemoteViews(PackageName, Resource.Layout.custom_notification);
    contentViews.SetImageViewResource(Resource.Id.icon, Resource.Drawable.heart_save);
    contentViews.SetTextViewText(Resource.Id.tv_left, "Custom title");
    contentViews.SetTextViewText(Resource.Id.tv_right, "Custom content");
    
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.SetOngoing(true)
            .SetSmallIcon(Resource.Drawable.check)
            .SetContentTitle("App is running in background")
            .SetPriority(1)
            .SetContent(contentViews) // here add custom RemoteViews 
            .SetCategory(Notification.CategoryService)
            .Build();
            
    StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
    

    custom_notification.xmllayout 文件夹中:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="vertical"
        android:paddingStart="48dp"
        android:paddingEnd="48dp">
    
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentStart="true"
            android:src="@android:drawable/ic_lock_idle_alarm" />
        
        <TextView
            android:id="@+id/tv_left"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center_vertical"
            android:text="Left" />
    
    
        <TextView
            android:id="@+id/tv_right"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:gravity="center_vertical"
            android:text="Right" />
    </RelativeLayout>
    

    效果:

    【讨论】:

    • 感谢您的时间和帮助,但这不是我想要的,通知图标在顶部栏上显示为白色,我希望它看起来像 facebook 或 youtube 图标栏一样着色
    • @XAMMOT2 好的,您是否尝试过在物理设备上进行测试?在我测试的设备中,它总是显示彩色图标。
    猜你喜欢
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多