Skip to content

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

@jeyraof

Description

@jeyraof

[REQUIRED] Step 2: Describe your environment

  • Operating System version: macOS Ventura 13.5.2
  • Firebase SDK version: 4.12.1
  • Library version: ?
  • Firebase Product: messaging (auth, database, storage, etc)

[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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions