Android通知图标显示异常:标准创建流程下图标不符求助
Hey there! Let's walk through why your custom notification icon isn't showing up, and that unexpected icon is appearing instead. Here are the most common fixes to check:
1. Double-Check Your Icon's Name & Location
When you created the icon via File > New > Image Asset, make sure:
- The generated icon's filename exactly matches
ic_action1(check all yourdrawable-*folders—mdpi, hdpi, etc.—for this file). The Image Asset wizard might default to a name likeic_notificationif you didn't explicitly change it. - The icon is saved in
res/drawabledirectories, notres/mipmap(mipmap is reserved for launcher icons, not notifications).
2. Ensure Your Icon Follows Android's Notification Rules
Starting from API 21, Android enforces strict rules for notification icons:
- They must be white with a transparent background. If your icon has color or a non-transparent background, the system will automatically replace it with your app's launcher icon (the "other icon" you're seeing).
- When using the Image Asset wizard, make sure you select Notification Icon as the icon type, set the foreground color to white, and set the background to transparent.
3. Fix Notification Channel Setup
For API 26+, notifications require a valid channel, and if you don't create one properly, the system might fall back to default settings (including using the launcher icon). Add this code before sending your notification to create the channel:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // Fix the typo in "chanal_Id" to "channel_Id" if you can—consistency helps! NotificationChannel channel = new NotificationChannel("default_chanal_id", "Default Channel", NotificationManager.IMPORTANCE_DEFAULT); // Explicitly set the channel's icon to match your notification icon channel.setIcon(R.drawable.ic_action1); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); }
Note: Your variable chanal_Id has a typo, but as long as you use the same string when creating the channel, it will work—though fixing it avoids future confusion.
4. Clear Caches & Rebuild
Android Studio's resource cache can sometimes prevent new icons from being packaged correctly. Try these steps:
- Click Build > Clean Project
- Then click Build > Rebuild Project
- Uninstall the existing app from your device/emulator, then reinstall it
5. Test With a New Notification ID
If you've previously sent a notification with ID 1, the system might be reusing old settings. Try changing the ID in your notify call to something new, like:
notificationManager.notify(999, notification);
Go through these steps one by one, and your custom notification icon should start showing up as expected!
内容的提问来源于stack exchange,提问作者Dmitri Ivanov

