douya1974 2016-07-25 16:30
浏览 245
已采纳

如何最好地使用Go使用FCM / GCM发送复杂数据

Both the FCM and GCM documentation give the structure of the data payload as a map[string]string (although google's GCM package implements it at a map[string]interface{})

However, there are many cases where a simple flat key:value structure doesn't meet the needs of an application. Some examples are when a slice of values is needed, or when another non-trivial struct needs to be sent.

What would be the cleanest way of sending more complicated data structures as a map[string]string?


Conclusion: I have marked the answer by fl0cke as correct given that it provides a solution to sending complex data with FCM / GCM using Go. However, it is clear from the FCM documentation, that the intention is for the data to be key:value string pairs moving forward, and so to be sure that nothing gets broken in the future, I will be sticking to simple key:value string pairs.

  • 写回答

1条回答 默认 最新

  • dtwxt88240 2016-07-25 17:30
    关注

    According to this answer, it is possible to send nested data with FCM/GCM. To do that, you can write your own FCM Client or fork google's implementation and change the type definition of Data from

    type Data map[string]interface{}
    

    to

    type Data interface{}
    

    And plug in any type that is JSON serializable (e.g. nested structs).


    It is also possible to send the data via a JSON string without changing the type definition of Data:

    // first marshal your complex data structure
    complexData := someComplexStruct{...}
    b, _ := json.Marshal(complexData)
    
    // then assign the returned json string to one key of your choice
    data := map[string]interface{}{"key":string(b)}
    

    You probably have to unquote the json string before parsing it on the client.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题