admin管理员组

文章数量:1414604

According to the react-native-fcm package you can include a custom nested object within the data object for a FCM messaging payload.

according to this post by the package author

Like this:

var payload = {
    data: {
        custom_notification: {
            title: 'title',
            body: 'body',
            priority: 'high',
            id: 'id',
            group: 'group'
        }
    }
};

This is for the purpose of receiving heads up notifications in all app states, which will not happen if you do just a notification payload or data payload.

When I implement this in my cloud function I get the following error:

Error: Messaging payload contains an invalid value for the "data.custom_notification" property. Values must be strings.

So I'm at a loss as to how others can be using this successfully?

I wonder if there's some issue going on with my environment or something as the following test payload which was given to me by firebase support (and is in the docs) errors out:

var payload = {
"to":"FCM_TOKEN",
"data": {
"type":"MEASURE_CHANGE",
"body": "test body",
"title": "test title",
"color":"#00ACD4",
"priority":"high",
"id": "id",
"show_in_foreground": true
}
};

I get the following error:

Error sending message stringify: {"code":"messaging/invalid-payload","message":"Messaging payload contains an invalid \"to\" property. Valid properties are \"data\" and \"notification\"."}

Been at this for days so hopefully I can get a bit of help on this.

Thanks in advance!

According to the react-native-fcm package you can include a custom nested object within the data object for a FCM messaging payload.

according to this post by the package author

Like this:

var payload = {
    data: {
        custom_notification: {
            title: 'title',
            body: 'body',
            priority: 'high',
            id: 'id',
            group: 'group'
        }
    }
};

This is for the purpose of receiving heads up notifications in all app states, which will not happen if you do just a notification payload or data payload.

When I implement this in my cloud function I get the following error:

Error: Messaging payload contains an invalid value for the "data.custom_notification" property. Values must be strings.

So I'm at a loss as to how others can be using this successfully?

I wonder if there's some issue going on with my environment or something as the following test payload which was given to me by firebase support (and is in the docs) errors out:

var payload = {
"to":"FCM_TOKEN",
"data": {
"type":"MEASURE_CHANGE",
"body": "test body",
"title": "test title",
"color":"#00ACD4",
"priority":"high",
"id": "id",
"show_in_foreground": true
}
};

I get the following error:

Error sending message stringify: {"code":"messaging/invalid-payload","message":"Messaging payload contains an invalid \"to\" property. Valid properties are \"data\" and \"notification\"."}

Been at this for days so hopefully I can get a bit of help on this.

Thanks in advance!

Share Improve this question edited Aug 27, 2017 at 8:11 AL. 37.8k10 gold badges147 silver badges285 bronze badges asked Aug 27, 2017 at 6:41 rt_rt_ 1,1952 gold badges16 silver badges29 bronze badges 1
  • try to stringify the payload using: body: JSON.stringify({ your json object }) – Ganesh Cauda Commented Aug 27, 2017 at 7:54
Add a ment  | 

2 Answers 2

Reset to default 2

So I realised just now (after days of searching) that the package react-native-fcm is using a different send method than admin.messaging().sendToDevice(token, payload, options). I had been using that for a while now and didn't realize that it was not actually what was intended to be used with this library or atleast in this scenario. Mainly because everything was working fairly well using admin.messaging() up until I wanted heads up notifications in all app states.

The other method is like this

  sendData(token) {
    let body = {
        "to": token,
      "data":{
            "title": "Simple FCM Client",
            "body": "This is a notification with only DATA.",
            "sound": "default",
            "click_action": "fcm.ACTION.HELLO",
            "remote": true
        },
        "priority": "normal"
    }

    this._send(JSON.stringify(body), "data");
  }

  _send(body, type) {
    let headers = new Headers({
        "Content-Type": "application/json",
        "Content-Length": parseInt(body.length),
      "Authorization": "key=" + FirebaseConstants.KEY
    });

    fetch(API_URL, { method: "POST", headers, body })
        .then(response => console.log("Send " + type + " response", response))
        .catch(error => console.log("Error sending " + type, error));
  }

You can use nested objects within the data object using this method. The documentation is not super clear on this unfortunately, I didn't even realise there was an example until now. Of course this could have just been me.

When using the data message payload, it is stated to use key value pairs that are String, so what you could do is have the value of your custom_notification as JSON String by enclosing them in " ".

For the sample payload provided, are you actually using the FCM_TOKEN in the to parameter? You're supposed to replace it with an actual token.

本文标签: javascriptFCM message payload errorsNested object within data object errors outStack Overflow