firebase / firebase-admin-go

Firebase Admin Go SDK
Apache License 2.0
1.12k stars 239 forks source link

Way to unmarshal `Message` from json string (apns.payload.aps.*) #590

Open jeyraof opened 8 months ago

jeyraof commented 8 months ago

[REQUIRED] Step 2: Describe your environment

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

I've deal with json like:

{
  "token": "$FCM_TOKEN",
  "data": {
    "title": "data.title",
    "body": "data.body"
  },
  "notification": {
    "title": "notification.title",
    "body": "notification.body"
  },
  "apns": {
    "payload": {
      "aps": {
        "content-available": false,
        "sound": "1",
        "mutable-content": true,
        "alert": {
          "title": "apns.payload.aps.alert.title",
          "subtitle": "apns.payload.aps.alert.subtitle",
          "body": "apns.payload.aps.alert.body"
        }
      }
    }
  }
}

For use Message's interface, I have to unmarshal this json to Message struct. So I tried to:

// 1st way
message := &messaging.Message{}
err = message.UnmarshalJSON(messageJsonString)
if err != nil {
    fmt.Println(err.Error())
}

// 1st way's error:
json: cannot unmarshal bool into Go struct field APNSConfig.apns.payload of type int
// 2nd way
message := &messaging.Message{}
err = json.Unmarshal(messageJsonString)
if err != nil {
    fmt.Println(err.Error())
}

// 2nd way's error:
json: cannot unmarshal bool into Go struct field APNSConfig.apns.payload of type int

I did some testing and found that it works fine without "content-available" and "mutable-content" in the json. How can I parse content-available and mutable-content? This fields are from apple reference