You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin中如何设置自定义通知图标?Firebase Messaging使用遇阻

Xamarin Android自定义通知SetSmallIcon资源引用错误解决办法

你代码里调用SetSmallIcon时的资源引用写法有误,Xamarin中引用Drawable资源不需要带文件后缀(比如.png),因为Resource.Drawable下的成员是编译后自动生成的int类型常量,直接使用资源文件名(不带后缀)即可。

修正后的代码

NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, channelId)
    .SetContentIntent(pendingIntent)
    .SetContentTitle(pTitle)
    .SetContentText(pBody)
    .SetAutoCancel(true)
    .SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.abc_btn_check_material))
    .SetSmallIcon(Resource.Drawable.icon_test) // 移除.png后缀
    .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);

额外注意事项

  • 确认icon_test.png已放置在Android项目的Resources/Drawable(或对应分辨率的Drawable子目录,如drawable-hdpi、drawable-xhdpi等)下
  • 检查该图片文件的生成操作是否设置为AndroidResource(右键文件→属性→生成操作,必须选择此选项,否则资源不会被编译到Resource类中)
  • 资源文件名需符合Android规范:仅允许小写字母、数字、下划线,不能包含大写字母或特殊字符

内容的提问来源于stack exchange,提问作者emmaprofemx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 04:57:04