Flutter awesome_notifications类WhatsApp通知显示发送者头像
实现方法
在构造NotificationContent时新增largeIcon参数,直接传入推送数据中的sender_profile_pic字段即可。NotificationLayout.Messaging布局下,largeIcon对应通知左侧的头像展示位,配置后会自动覆盖默认的用户名首字母占位。
修改后的核心代码:
await AwesomeNotifications().createNotification( content: NotificationContent( id: UniqueKey().hashCode, groupKey: message.data["senderUid"], channelKey: 'basic_channel', title: message.data["title"], body: message.data["body"], summary: message.data["body"], notificationLayout: NotificationLayout.Messaging, // 配置发送者头像 largeIcon: message.data["sender_profile_pic"], ), );
适配注意事项
- Android端若头像使用http明文链接,需额外配置网络安全规则允许对应域名的明文访问,否则头像会加载失败回退为首字母占位,建议优先使用https头像链接。
- iOS端要求头像地址必须为可直接访问的https格式,系统会自动拉取资源渲染,无需额外配置。
- 若使用应用内置的资源图片作为头像,需要传入带
resource://前缀的资源路径,例如resource://drawable/ic_default_avatar。 - 若在后台隔离环境(如Firebase后台消息回调)中触发通知,建议提前将头像下载到本地临时目录,将本地文件路径传入
largeIcon参数,避免后台环境网络权限限制导致头像加载失败。 - 同个
groupKey下的多条通知折叠后,默认取最新一条通知的largeIcon作为折叠组头像,需要自定义群组头像可在分组配置中单独设置。

内容的提问来源于stack exchange,提问作者PeakGen
相关产品推荐
相关产品推荐

