Notification Icon not display properly Xamarin Android

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)
        {
        }
}

Envoironments:

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

Best Answer

Answers

  • ivanxamarinivanxamarin Member ✭✭

    You should follow Status bar icon guidelines: https://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar

    There is a limited grey palette that can be used.

  • pingpong.6106pingpong.6106 USMember ✭✭
    edited January 7

    @ivanxamarin,

    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.

    It is strange.

  • ivanxamarinivanxamarin Member ✭✭

    @pingpong.6106
    Did you change testing device? Icon display in android are dependent on device specific OS or even the chosen theme. On my device one theme shows one icon and the other shows another.

  • pingpong.6106pingpong.6106 USMember ✭✭
    edited January 7

    @ivanxamarin,

    Both apks are installed on the same device, and OS. Again, the second apk was created based on the first branch.

  • YorkGoYorkGo CNMember, Xamarin Team Xamurai
    edited January 8

    Notification Icon not display properly Xamarin Android

    This is know issue in Android: Why do icons set with Notification.Builder.setSmallIcon in Android Lollipop show as a white square?

    As the document said:

    Status bar icons are composed simply of white pixels on a transparent backdrop, with alpha blending used for smooth edges and internal texture where appropriate.

    Update or remove assets that involve color. The system ignores all non-alpha channels in action icons and in the main notification icon. You should assume that these icons will be alpha-only. The system draws notification icons in white and action icons in dark gray.

    PS: I download icon from: https://www.iconfont.cn/search/index?q=notification and it works fine.

  • ivanxamarinivanxamarin Member ✭✭

    @pingpong.6106

    Well, it is strange that it works with one apk and not with another with same code on the same device, but you should follow guidelines anyway. There has to be reason for strange behavior, but it won't be important after you create proper icon.

  • pingpong.6106pingpong.6106 USMember ✭✭
    edited January 9

    I fixed it by changing below

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

    which results in beblow:

    android:minSdkVersion="15"

    What does it mean by "Use Compile using SDK version"? What target version is being used when installed on Lollipop?

Sign In or Register to comment.