0

I have problem setting up a notification image to display properly.

Below is the original image, with file path:

myApp\Resources\drawable\Icon.png

Dimensions: 72x72pixels, 32bit depths

enter image description here

The problem:

It displays like below.

enter image description here

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. Once the code was created for a second branch, the issue occurs on the second branch.

Update 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"? What target version is being used when installed on Lollipop?

Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32
Pingpong
  • 7,681
  • 21
  • 83
  • 209
  • 1
    Did you update your compile/target sdk version on the new branch? If so, was it below API 21 and is now API 27? – SharpMobileCode Jan 08 '19 at 04:26
  • Did the answers help you? – Leo Zhu Jan 08 '19 at 07:33
  • Please see update 2 – Pingpong Jan 08 '19 at 20:57
  • 1
    For most developers, we do not recommend setting the Target Android version to Use Compile using SDK version.In general, the Target Android Version should be bounded by the Minimum Android Version and the Target Framework. That is: Minimum Android Version <= Target Android Version <= Target Framework ! In your update2,if you select Use Compile using SDK version, the Target Android version will be the same as the Target Framework setting(Project Properties -> Application). – Leo Zhu Jan 09 '19 at 02:06
  • You said "the Target Android version will be the same as the Target Framework setting". if I change Target Android Version to 8.0 from Use Compile using SDK version, which is the same as Target Framework, the icon does not work. This contradicts to what you said. – Pingpong Jan 09 '19 at 12:29
  • I tested it myself. Indeed, after using the Use Compile using SDK version, Target Android version is not consistent with the Target Framework, but with Minimum Android version.It doesn't match the official documentation. It's weird – Leo Zhu Jan 10 '19 at 04:55
  • I think it may be the same as Minimum Android version. But I don't know how to verify it. – Pingpong Jan 11 '19 at 14:52
  • 1
    i have tested it and reported, it is the same as Minimum Andorid version,you could verify it like this : Project Properties -> Android Manifest -> Target Android Versin to "Use Compile using SDK version" , then in the code : `var targetSdkVersion = ApplicationInfo.TargetSdkVersion;` , you could print the `targetSdkVersion` – Leo Zhu Jan 14 '19 at 06:50
  • Can you share the reported ticket? – Pingpong Jan 14 '19 at 13:30
  • 1
    you could have a look at :https://github.com/xamarin/xamarin-android/issues/1163#issuecomment-401928223 – Leo Zhu Jan 15 '19 at 05:33

1 Answers1

1

Because Google has made restrictions on android5.0 and above (not including 5.0), in order to unify the system style. 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)

2.Set setSmallIcon, respectively(Make another white icon with a transparent background)

     

 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

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • I will also add that inside the if statement for Lollipop and above, you can also call `notificationBuilder.setColor()` to apply a tint color to the white icon. This only works for API 21 (Lollipop) and above. – SharpMobileCode Jan 08 '19 at 05:04
  • Please note that it supports both Lollipop and Kitkat – Pingpong Jan 09 '19 at 15:16
  • Yes your code does support both, but I was talking the `notificationBuilder.setColor()` method. It can be used to tint the white icon. This method is only available for Lollipop and above, as stated in the documentation. https://developer.android.com/reference/android/app/Notification.Builder#setColor(int) – SharpMobileCode Jan 09 '19 at 16:46