- 确保图标资源文件被正确命名并放置在drawable文件夹下。
- 使用
NotificationCompat.Builder
的 setAutoAction()
方法设置操作项时,确保将正确的资源ID传递给 setCustomAction()
方法的第二个参数。
例如:
NotificationCompat.Action customAction = new NotificationCompat.Action.Builder(
R.drawable.ic_custom_action, "Custom Action",
customActionPendingIntent).build();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("My Notification Title")
.setContentText("This is my notification content.")
.setAutoAction(customAction);
- 如果您是使用 RemoteViews 创建自定义通知布局,请确保使用
setImageViewResource()
方法将正确的图标资源 ID 分配给操作项图标视图。例如:
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_custom_notification);
remoteViews.setImageViewResource(R.id.custom_action_icon, R.drawable.ic_custom_action);
// Set custom action
Intent customActionIntent = new Intent(this, MyBroadcastReceiver.class);
customActionIntent.setAction("com.example.CUSTOM_ACTION");
PendingIntent customActionPendingIntent = PendingIntent.getBroadcast(this, 0, customActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.custom_action_button, customActionPendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("My Notification Title")
.setContentText("This is my notification content.")
.setCustomContentView(remoteViews);