简体   繁体   English

在 Lollipop 上使用 Xamarin Android 无法正确显示通知图标

[英]Notification Icon not display properly with Xamarin Android on Lollipop

I have problem setting up a notification image to display properly.我在设置通知图像以正确显示时遇到问题。

Below is the original image, with file path:下面是原始图像,文件路径:

myApp\\Resources\\drawable\\Icon.png myApp\\Resources\\drawable\\Icon.png

Dimensions: 72x72pixels, 32bit depths尺寸:72x72 像素,32 位深度

在此处输入图片说明

The problem:问题:

It displays like below.它显示如下。

在此处输入图片说明

Below is the code.下面是代码。 The code seems working correctly for previous version which displays it properly.该代码似乎可以正常显示它的先前版本。

public class MyServiceConnection : Java.Lang.Object, IServiceConnection
{
    private string _appName;
    public MyServiceConnection(string appName)
    {
        _appName = appName;
    }
    public void OnServiceConnected(ComponentName name, IBinder service)
    {
        var intent = new Intent(Application.Context, typeof(ManifestActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent);
        var notificationBuilder = new NotificationCompat.Builder(Application.Context)

            .SetSmallIcon(Resource.Drawable.Icon)
            .SetContentTitle(_appName)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);
        var notificationManager = NotificationManager.FromContext(Application.Context);
        var notification = notificationBuilder.Build();
        notificationManager.Notify(1, notification);
        var serviceBinder = service as MyService.Binder;
        serviceBinder.StartForeground(1, notification);
    }
    public void OnServiceDisconnected(ComponentName name)
    {
    }
}

Environments:环境:

android:minSdkVersion="15" android:targetSdkVersion="27" 

Update更新

As I mentioned, the same code and same png file are used and working now on a separate apk.正如我所提到的,使用相同的代码和相同的 png 文件,现在在单独的 apk 上工作。 Once the code was created for a second branch, the issue occurs on the second branch.一旦为第二个分支创建了代码,问题就会出现在第二个分支上。

Update 2更新 2

I fixed it by changing below我通过在下面更改来修复它

Project Properties -> Android Manifest -> Target Android Version to "Use Compile using SDK version"

which results in below:结果如下:

android:minSdkVersion="15" 

What does it mean by "Use Compile using SDK version"? “使用使用 SDK 版本编译”是什么意思? What target version is being used when installed on Lollipop?安装在 Lollipop 上时使用的是什么目标版本?

Because Google has made restrictions on android5.0 and above (not including 5.0), in order to unify the system style.因为谷歌对android5.0及以上(不包括5.0)做了限制,为了统一系统风格。 After that, the icon in the status bar can't use a picture with rich colors at will.之后状态栏的图标就不能随意使用色彩丰富的图片了。 It can only appear in two colors, white and transparent.它只能以两种颜色出现,白色和透明。

You can do it two ways:您可以通过两种方式进行:

1.Color icons are displayed by lowering the targetSdkVersion method, but lowering the targetSdkVersion method is not supported.(not recommended) 1.通过降低targetSdkVersion方法显示彩色图标,但不支持降低targetSdkVersion方法。(不推荐)

2.Set setSmallIcon, respectively(Make another white icon with a transparent background) 2.分别设置setSmallIcon(制作另一个透明背景的白色图标)

 if(Android.OS.Build.VERSION.SdkInt> BuildVersionCodes.Lollipop){         
     // white icon with a transparent background
     notificationBuilder.SetSmallIcon(Resource.Drawable.ic_aphla_logo);
  } else {
     // icon image
     notificationBuilder.SetSmallIcon(Resource.Drawable.ic_logo);
 }

You can refer to this: Notification bar icon turns white in Android 5 Lollipop可以参考这个: Android 5 Lollipop 通知栏图标变白

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

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