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 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题
  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 echarts图表制作