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

如何在fcm-django推送通知中添加message_type与link额外参数?

在fcm-django中给Firebase推送添加自定义额外参数

fcm-django的send_message方法不支持直接传入type、link这类自定义关键字参数,但可以通过data参数传递所有自定义字段——这是FCM规范里专门用来承载业务自定义数据的字段。

正确的调用方式

把message_type和link打包成字典传入data参数即可:

# 组装自定义数据
custom_payload = {
    'type': message_type,
    'link': link
}

# 发送推送
devicesGCM.send_message(
    body=body,
    title=title,
    data=custom_payload
)

结合你的Notification模型使用

如果是从模型实例读取数据发送,可以直接关联字段(同时处理空值情况):

# 假设你有一个Notification实例:notification
custom_payload = {}
if notification.message_type:
    custom_payload['type'] = notification.message_type
if notification.link:
    custom_payload['link'] = notification.link

devicesGCM.send_message(
    body=notification.body,
    title=notification.title,
    data=custom_payload
)

客户端接收说明

FCM会把data里的内容原封不动传给客户端,你需要在客户端做对应解析:

  • Android:在FirebaseMessagingService的onMessageReceived方法中,通过remoteMessage.getData().get("type")和remoteMessage.getData().get("link")获取值
  • iOS:前台接收时从userInfo字典提取;后台接收需确保配置了正确的推送权限,在didReceiveRemoteNotification方法中读取对应键值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 16:13:18