如何在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
相关产品推荐
相关产品推荐

