简体   繁体   中英

Android notification icon is a white circle

I've got a strange problem with notification icon today.

It looks like this : 在此处输入图片说明 (the white circle ...)

Did I do something bad ?

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.icon_notification)
                .setContentTitle(this.getString(R.string.notification_title))
                .setContentText(this.getString(R.string.notification_text))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

Here is my icon image (freshly downloaded from here https://material.io/icons/#ic_photo ) : http://image.noelshack.com/fichiers/2016/44/1478185219-icon-notification.png

Did I miss something ?

For the record, I'm using SDK 24 and only created the hdpi resource folder for now.

Edit #1 : I've added the ldpi , mdpi and xhdpi icons, nothing change ...

Edit #2 : For more precision, I'm trying to create this notification from a service ... FCM messaging service ...

If your compileSDKversion is above 20 then notification icon should be a white-on-transparent background image. Otherwise the image will be rendered as a white colored image.

Please go through the below link too for guidelines to create the icon

https://www.google.com/design/spec/patterns/notifications.html

and also the notification icon generator.

https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.space.trim=1&source.space.pad=0&name=ic_stat_example

You must use a notification icon with no background. Android will add the circle background.

You can set background color with

.setColor(context.getResources().getColor(R.color.colorPrimary))

to match your app indentity.

Icon inside will remain white and circle will get the color you defined.

在 Android Studio 上 在系统栏 通知时

It seems to be a problem of cache during compilation ... The first image I was using was bad (fully colored), so I think my compilator created somekind of cache on the filename.

I work on Windows and did this : uninstall the app from my phone, invalidate all cache from Android sudio => at re-compilation, the icon was OK.

U need to have separate icon generated which will be white version of your launcher icon. U can use below link to generate such icon.

https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit

Note : You need to upload PNG image of your launcher icon with transparent background.

For setting icon u can have a method like this

private int getSmallIconForNotification(){
    return (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP)? R.mipmap.ic_stat_launcher : R.mipmap.ic_launcher;
}

Code usage:

private NotificationCompat.Builder createNotificationBuilder(){
    return new NotificationCompat.Builder(this)
            .setSmallIcon(getSmallIconForNotification())
            .setContentTitle("New Message")
            .setContentText("Hi there.....")
            .setAutoCancel(true);
}

Note:- If device has android version above 20 you have to generate icon with transparent background and while generating notification use this snippet

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP){
        currentapiVersion=R.mipmap.ic_notification_lolipop;
} else{
        currentapiVersion=R.mipmap.ic_launcher;
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(currentapiVersion)......

I had the same problem while the app is closed and this helped me

Unfortunately this was a limitation of Firebase Notifications in SDK 9.0.0-9.6.1. When the app is in the background the launcher icon is use from the manifest (with the requisite Android tinting) for messages sent from the console.

With SDK 9.8.0 however, you can override the default! In your AndroidManifest.xml you can set the following fields to customise the icon and color:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/notification_icon" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/google_blue" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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